scala - 带有多个参数列表的方法的 “missing argument list” -error消息中有违反直觉的建议吗?

标签 scala compiler-errors

考虑以下代码片段:

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/

相关文章:

scala - Scala 集合如何能够从映射操作返回正确的集合类型?

scala - 在 Scala 中从字符串创建复杂数据

java - 运行 Java 给出 "Error: could not open ` C :\Program Files\Java\jre6\lib\amd64\jvm. cfg'"

Java/Scala - 响应式(Reactive)流/枚举如何连续读取输入流直到 read() 返回 -1

c++ - VC++ 错误 : error C1083: Cannot open source file: '=0x0401' : No such file or directory

c - 为什么下面的代码会显示错误?

c - ncurses在Windows 8.1上编译时出现错误

java - Maven 编译器插件不支持的类文件主要版本 60

Scala 参与者和共享状态

c - 包含 2 位十六进制值的数组应使用哪种 C 数据类型?