scala 下划线解析报错: missing parameter type for expanded function
本文首先介绍下eta-expansion概念,以及其对下划线的expand的解析规则和匿名函数简写注意事项,最后通过例子解析加深读者的理解
eta-expansion概念: 把 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: scala> List(1,2,3).map(_*2) res6: List[Int] = List(2, 4, 6)
scala> List(1,2,3).map(_*2+1) res14: List[Int] = List(3, 5, 7)
但是吧*2 换成(2) 则出错,因为根据就近原则 _2 会在最近的括号中解析成匿名函数 为(x$1) => x$1.$times(2), 变成为(x=>x2) +1 , 而期望的是(x)=>(x2)+1 scala> List(1,2,3).map((*2)+1) <console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$times(2)) List(1,2,3).map((*2)+1) ^
如果括号内只有_ 本身,则本身构不成一个表达式expressions,所以如下是允许的 scala> List(1,2,3).map(()+1) res8: List[Int] = List(2, 3, 4)
例子2:
scala> List(1,2,3,4).foreach(print _) 1234
_本身构不成一个表达式:expressions,所以如下是允许的
scala> List(1,2,3,4).foreach(print(_)) 1234
(.toString) 是一个在括号()里的表达式,会expand解析成匿名函数,而print期望的String类型的值,所以编译报错 scala> List(1,2,3,4).foreach(print(.toString)) <console>:8: error: missing parameter type for expanded function ((x$1) => x$1.toString) List(1,2,3,4).foreach(println(_.toString)) ^
例子3:
val myStrings = new ArrayString// 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.
**keywords:error: missing parameter type for expanded function **
Eta Expansion
The usage of placeholder syntax for anonymous functions is restricted to expressions.
参考如下:
This has already been addressed in a related question. Underscores extend outwards to the closest closing Expr