Scala 中的匿名函数,错误取决于符号

Anonymous function in Scala, error depending on notation

我正在尝试区分两个数组。具体来说,我有一个数组包含一些字符串,另一个数组包含我想从第一个数组中清除的索引。起初我尝试使用带有“_”的匿名函数注解,但它没有用,我很难理解为什么。 这是一个例子:

val a1 = Array("word1","word2","word3")
val ind = Array(1,2)
val a2 = a1.zip(a1.indices)

val not_working = a2.filterNot( ind.contains(_._2) )

> console>:15: error: missing parameter type for expanded function
> ((x) => x._2)
>         a2.filterNot( ind.contains(_._2) )
>                                    ^ <console>:15: error: type mismatch; 
> found   : Boolean  required: ((String, Int)) => Boolean
>         a2.filterNot( ind.contains(_._2) )

我的假设是 _ 对应于例如 ("word1",0) 或 ("word2",1) 元组。但是我不明白报错,为什么这里得到的是Boolean类型?我虽然它会得到元组的第二部分。

为了它,我尝试更明确地重写它,并且成功了。这是一个符合我预期的示例,在我天真的眼中,我唯一改变的是符号:

val working = a2.filterNot( x => ind.contains(x._2) )

我很难理解两个示例之间的区别。为什么第二个有效?

有两个错误叠加在一起:

val not_working = a2.filterNot( ind.contains(_._2) )

第一个错误是 missing parameter type for expanded function。本质上,_ 语法不能在任何地方使用,这就是其中一种情况。 (在这种情况下,这是因为占位符语法不能在嵌套括号内使用。

第二个错误是编译器不理解你试图使用占位符语法的结果,但它仍然知道 ind.contains returns 是一个布尔值。这在错误消息中指出:found: Boolean。编译器认为布尔值传递给 filterNot,但 filterNot 需要一个函数 T => Boolean.

修复语法问题后,编译器会正确解析表达式并编译您的示例。

正如它提到的 here and here 和其他多个地方

The placeholder syntax for anonymous functions replaces the smallest possible containing expression with a function.

所以

a2.filterNot( ind.contains(_._2) )

将被脱糖为

a2.filterNot( ind.contains(x => x._2) )