I am using _
as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want.
List(1,2,3) foreach println(_:Int) //error !
List(1,2,3) foreach (println(_:Int)) //work
List(1,2,3) foreach(println(_:Int)) //work
Using -Xprint:typer
I can see Scala transforms the first one into "a big anonymous function":
x$1 => List(1,2,3) foreach(println(x$1:Int))
the worked 2th 3th are right transformation into what I want.
... foreach (x$1 => println(x$1:Int))
Why this? What's the rule ?
Simple rules to determine the scope of underscore:
So, by the rule #1, instead of println((x: Int) => x)
, the scope will be placed outside (including) println
.
By rule #2, the latter two examples will have the function delimited by parenthesis, so (x => println(x: Int))
.
By rule #3, the first example will be the whole expression, as there are no delimiting parenthesis.