Extending a partially implemented partial function in scala

I use the Akka acting library here. The actor library defines a partial “receive” function that an actor who extends the “actor” must implement in order to deal with various messages. I am creating a feature hierarchy for my application where the “clockActor” trait extends the actor, and “MasterClock” and “SubClock” extend the “clockActor”. I want to add the general functionality of the clock to the reception function of the "clock", but then how to add the additional functionality of the reception function in the main and auxiliary measures?

In short, I need a way to add additional case arguments to a partial function. Ideas?

+6
source share
2 answers

As already mentioned, you can easily compose PartialFunctions with orElse

 trait ClockActor { def commonOp = { case ... => ... } } class MasterClock extends Actor with ClockActor { def receive = commonOp orElse masterOp def masterOp = { case ... => ... } } class SubClock extends Actor with ClockActor { def receive = commonOp orElse subOp def subOp = { case ... => ... } } 
+12
source

One thing that comes to mind is to do something like this:

 trait ClockActor { def pf:PartialFunction[String, Boolean] = { case "a" => true case v if(_pf.isDefinedAt(v)) => _pf.apply(v) } def _pf:PartialFunction[String, Boolean] = Map.empty } object MasterClock extends ClockActor { override def _pf:PartialFunction[String, Boolean] = { case "b" => false } println(pf("a")) println(pf("b")) } 

which will output:

 scala> MasterClock true false 

The value true comes from the definition in the partial function Trait ClockActor , false comes from the Object MasterClock .

+1
source

All Articles