scalacheat
scalacheat copied to clipboard
anonymous function: block style returns last expression (error)
When trying the listed example I get an error:
scala> (1 to 5).map { val x=_*2; println(x); x }
<console>:9: error: missing parameter type for expanded function ((x$1) => x$1.$times(2))
(1 to 5).map { val x=_*2; println(x); x }
Using this code it works:
(1 to 5).map { y => val x =y*2; println(x); x }
2
4
6
8
10
res23: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)