把 x => func(x) 简化为 func _ 或 func 的过程称为 eta-conversion把 func 或 func _ 展开为 x => func(x) 的过程为 eta-expansion
Eta Expansion的就近expand解析原则:
Underscores extend outwards to the closest closing Expr : top-level expressions or expressions in parentheses
匿名函数简写注意事项:
所以当匿名函数有多个括号嵌套的时候,不要使用_的简写方式 ,而应该用普通匿名函数的书写方式。比如 (x:Int,y:Int)=> (x*2)+f(y+2)
例子解析:
例子1:
例子2:
例子3:
val myStrings = new Array[String](3)
// do some string initialization
// this works
myStrings.foreach(println(_))
// ERROR: missing parameter type for expanded function
myStrings.foreach(println(_.toString))
It expands to:
myStrings.foreach(println(x => x.toString))
You want:
myStrings.foreach(x => println(x.toString))
The placeholder syntax for anonymous functions replaces the smallest possible containing expression with a function.
This has already been addressed in a related question. Underscores extend outwards to the closest closing Expr
: top-level expressions or expressions in parentheses.
(_.toString)
is an expression in parentheses. The argument you are passing to Exception
in the error case is therefore, after expansion, the full anonymous function (x$1) => x$1.toString
of type A <: Any => String
, while Exception
expects a String
.
In the println
case, _
by itself isn't of syntactic category Expr
, but (println (_))
is, so you get the expected (x$0) => println(x$0)
.