Simple rules to determine the scope of underscore:
- If the underscore is an argument to a method, then the scope will be outside that method, otherwise respective the rules below;
- If the underscore is inside an expression delimited by () or {}, the innermost such delimiter that contains the underscore will be used;
- All other things being equal, the largest expression possible will be used.
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.