关于SO上的这个错误消息有几个问题,但他们似乎都没有涉及到这个问题。Scala论据过载中的匿名函数的类型
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可以清楚地看到我与字符串匹配,但无法推断出参数类型。