为了补充其他答案,下面是一些例子,说明在使用'_'作为占位符参数时,在某些情况下为什么会出现“缺少参数类型”。
Scala的类型推断根据上下文考虑表达式的'expected'类型。如果没有上下文,则不能推断参数的类型。在错误消息中注意_
的第一个和第二个实例被编译器生成的标识符x$1
和x$2
所取代。
scala> _ + _
<console>:5: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
_ + _
^
<console>:5: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
_ + _
^
添加类型归属到整个表达式提供足够的上下文来帮助inferencer:
scala> (_ + _) : ((Int, Int) => Int)
res3: (Int, Int) => Int = <function2>
或者,你可以添加一个类型归属到每个参数占位符:
scala> (_: Int) + (_: Int)
res4: (Int, Int) => Int = <function2>
在下面的函数调用中提供了类型参数,上下文是不可信的,并且函数类型被推断出来。
scala> def bar[A, R](a1: A, a2: A, f: (A, A) => R) = f(a1, a2)
bar: [A,R](a1: A,a2: A,f: (A, A) => R)R
scala> bar[Int, Int](1, 1, _ + _)
res5: Int = 2
但是,如果我们要求编译器推断类型的参数,如果失败:
scala> bar(1, 1, _ + _)
<console>:7: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
bar(1, 1, _ + _)
^
<console>:7: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
bar(1, 1, _ + _)
^
我们能帮助它,但是,通过讨好的参数列表。这里,第一个参数列表(1, 1)
的参数告诉推论,类型参数A
应该是Int
。然后它知道参数f
的类型必须是(Int, Int) => ?)
,并且返回类型R
被推断为Int
,即整数加法的结果。您会在标准库中看到Traversable.flatMap
中使用的相同方法。
scala> def foo[A, R](a1: A, a2: A)(f: (A, A) => R) = f(a1, a2)
foo: [A,R](a1: A,a2: A)(f: (A, A) => R)R
scala> foo[Int, Int](1, 1) { _ + _ }
res1: Int = 2
scala> foo(1, 1) { _ + _ }
res0: Int = 2
@Scoobie为了强化这一点,下划线用于Scala中许多_different_目的。正如David解释的那样,在你的例子中每个用途实际上都有不同的含义。还有其他一些含义 - 在Scala中强调,是运算符超载导致问题的一个很好的例子。虽然起初我有问题,但我可以诚实地说,我从来没有想过通过某种方式来改进它。 – 2010-03-01 01:38:41