Search code examples
scalalambdaanonymous-functionpartial-applicationscala-placeholder-syntax

What are the rules to govern underscore to define anonymous function?


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 ?


Solution

  • Simple rules to determine the scope of underscore:

    1. If the underscore is an argument to a method, then the scope will be outside that method, otherwise respective the rules below;
    2. If the underscore is inside an expression delimited by () or {}, the innermost such delimiter that contains the underscore will be used;
    3. 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.