Lift在Comet Actors的实现中使用了一个PartialFunction,你通常会在你的类中使用这个:
override def lowPriority: PartialFunction[Any,Unit] = {
case MyCaseClass1(a) => do something here
case MyCaseClass2(a) => do something here
case AlwaysPresentCaseClass => default action
}
我想做的是,我不确定是否有可能分割部分函数,以便最后一个案例可以移动到trait中。
所以当我有一个新的彗星演员时,我只需:
class MyNewComet extends MyActorTrait {
override def lowPriority: PartialFunction[Any,Unit] = {
case MyCaseClass1(a) => do something here
case MyCaseClass2(a) => do something here
}
}
而MyActorTrait会有缺失的
case AlwaysPresentCaseClass => default action
您可以使用orElse
方法组合部分函数:
val f1: PartialFunction[Any, String] = {
case 22 => "hallo"
}
val f2: PartialFunction[Any, String] = {
case "rara" => "welt"
}
val f = f1 orElse f2 // f falls back to f2 if undefined in f1
f(22)
f("rara")
试试这个:
trait MyActorTrait extends /* whatever class provides lowPriority */ {
def default: PartialFunction[Any, Unit] = {
case AlwaysPresentCaseClass => default action
}
abstract override def lowPriority: PartialFunction[Any,Unit] =
super.lowPriority orElse default
}
唯一的问题是你不能做MyNewComet extends MyActorTrait
。相反,您可以使用class MyNewCometDefault extends MyNewComet with MyActorTrait
或new MyNewComet with MyActorTrait
。