Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 375
WeiChing 林煒清 Avatar asked Jan 24 '15 13:01

WeiChing 林煒清


People also ask

How do you define an anonymous function?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.

What do you need to create an anonymous function?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

Which function is also known as anonymous function?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

What is an anonymous function in Java?

What is an anonymous function? Anonymous function is a function define as not bound to an identifier. Because,These are a form of nested function in allowing access to variables in the scope of the containing function (non-local functions). So,This means anonymous functions need to be implemented using closures.


1 Answers

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.

like image 102
Daniel C. Sobral Avatar answered Oct 26 '22 10:10

Daniel C. Sobral