scalacheat icon indicating copy to clipboard operation
scalacheat copied to clipboard

anonymous function: block style returns last expression (error)

Open alkhatib opened this issue 10 years ago • 1 comments

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)

alkhatib avatar Mar 25 '14 16:03 alkhatib