使用"_"的Scala功能图

Pen*_*gin 2 scala

为什么(在Scala REPL中)我可以写,例如,

def double(d: Int) = 2*d
(0 until 10).zipWithIndex.map(i => double(i._1))
Run Code Online (Sandbox Code Playgroud)

要不就

(0 until 10).zipWithIndex.map(_._1)
Run Code Online (Sandbox Code Playgroud)

但我不能写

(0 until 10).zipWithIndex.map(double(_._1))
Run Code Online (Sandbox Code Playgroud)
error: missing parameter type for expanded function ((x$1) => x$1._1) (0 until 10).zipWithIndex.map(double(_._1))
Run Code Online (Sandbox Code Playgroud)

Deb*_*ski 11

Scala试图扩展_._1 内部 double.所以,它认为你想拥有

(0 until 10).zipWithIndex.map(double(i => i._1))
Run Code Online (Sandbox Code Playgroud)

但是,它也看到它i => i._1并不真正适合其中一个double参数类型,因此它会抱怨并要求您提供类型提示来帮助编译器.但是,在这种情况下,没有正确的类型定义,因此错误消息在那里是错误的.