"missing argument list" 错误消息中具有多个参数列表的方法的反直觉建议?



请考虑以下代码片段:

def foo(a: Int)(b: Int) = a + b
foo

它不会编译,并生成以下错误消息:

error: missing argument list for method foo
Unapplied methods are only converted to functions when 
a function type is expected.
You can make this conversion explicit by writing 
`foo _` or `foo(_)(_)` instead of `foo`.

foo _提示有效。但是如果我写表达式

foo(_)(_)

如上一条错误消息所示,我收到一条新的错误消息:

error: missing parameter type for expanded 
function ((x$1: <error>, x$2: <error>) => foo(x$1)(x$2))

这似乎相当违反直觉。

foo(_)(_)提示在什么情况下应该有帮助,它到底告诉我什么

(去除噪音;我越是继续编辑问题,它就越没有意义;科尔马是对的(

foo(_)(_)的类型是(Int, Int) => Int。因此,如果您指定该类型或在需要此类型的上下文中使用它,它将起作用:

scala> foo(_: Int)(_: Int)
res1: (Int, Int) => Int = $$Lambda$1120/1321433666@798b36fd
scala> val f: (Int, Int) => Int = foo(_)(_)
f: (Int, Int) => Int = $$Lambda$1121/1281445260@2ae4c424
scala> def bar(f: (Int, Int) => Int): Int = f(10, 20)
bar: (f: (Int, Int) => Int)Int
scala> bar(foo(_)(_))
res2: Int = 30

最新更新