The second one takes a call-by-name parameter, which is a different type than an Int.
scala> def foo(x: => Int): Int = x
foo: (x: => Int)Int
scala> foo _
res3: (=> Int) => Int = <function1>
foreach takes as an argument a function (Int) => Unit, and (=> Int) => Unit is not of type (Int) => Unit.
Call by name parameters are a little hack-y and aren’t really first class in scala. They can make some APIs cleaner, but they are currently restrictive. See SI-5787.
Why doesn’t this one compile? http://nurkiewicz.github.io/talks/2014/scalar/#/18
The second one takes a call-by-name parameter, which is a different type than an Int.
foreach takes as an argument a function (Int) => Unit, and (=> Int) => Unit is not of type (Int) => Unit.
Call by name parameters are a little hack-y and aren’t really first class in scala. They can make some APIs cleaner, but they are currently restrictive. See SI-5787.