我想定义使用部分函数处理消息的类的类层次结构。每个类都应该将它无法处理的消息推迟到它的超类。像这样的东西:
type Respond = PartialFunction[String, String]
abstract class HandlesNothing {
def respond: Respond = Map.empty // the empty response. Handles nothing
}
class HandlesA extends HandlesNothing {
override def respond = { case "a" => "The letter A" } orElse super.respond
}
class HandlesAAndB extends HandlesA {
override def respond = { case "b" => "The letter B" } orElse super.respond
}
你明白了。但是当我尝试这个时,我得到以下错误
<console>:21: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
override def respond = { case "b" => "The letter B" } orElse super.respond
^
当我明确给出 {...} 块的类型时,它可以工作:
class HandlesAAndB extends HandlesA {
override def respond = ({ case "b" => "The letter B" }:Respond) orElse super.respond
}
有没有办法实现这一点而不必显式指定部分功能块的类型?