【发布时间】:2017-02-01 20:35:54
【问题描述】:
关于 SO 上的此错误消息有很多问题,但似乎都与此问题无关。
The argument types of an anonymous function must be fully known. (SLS 8.5)
有问题的代码块试图模拟 Ruby 的块功能,另外还有一个好处是参数可以在过程中进行模式匹配。
object Block {
def apply(f: => Unit) = apply((_: String) => f)
def apply(f: String => Unit) = ???
}
def example() = {
Block { // Error!
case "A" => println("First letter of the alphabet")
case _ => println("Not the first letter of the alphabet")
}
}
即使向下一行,Scala 可以清楚地看到我正在匹配一个字符串,但它无法推断参数类型。
【问题讨论】:
标签: scala anonymous-function overloading