I would like to define a class hierarchy of classes that handle messages using a partial function. Each class should defer messages that it can not handle to its superclass. Something like this:
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
}
You get the idea. But when I try this I get the following error
<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
^
When I give the type of the {...} block explicitly, it works:
class HandlesAAndB extends HandlesA {
override def respond = ({ case "b" => "The letter B" }:Respond) orElse super.respond
}
Is there a way to achieve this without having to explicitly specify the type of the partial function block?