考虑以下代码片段:
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(_)(_)
-hint应该会有所帮助,它究竟告诉我什么?(消除了噪声;我越是不停地编辑问题,它所做的理解就越少;科尔玛是对的)
最佳答案
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
关于scala - 带有多个参数列表的方法的 “missing argument list” -error消息中有违反直觉的建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52413122/