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
}
我想做什么,而且我不确定是否可以拆分该部分功能,以便将最后一个案例移动到特征中。
所以,当我有一个新的彗星演员时,我只是这样做:
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
答案 0 :(得分:9)
您可以使用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")
答案 1 :(得分:0)
试试这个:
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
。