How do I intercept a PartialFunction? e.g. in actors, if I'd like to just print everything that comes into the following receive method before passing it onto the process method:
class MyActor extends Actor {
def receive : Receive = process
def process : Receive = {
case Some(x) => /* do one thing */ ()
case None => /* do another thing */ ()
case _ => /* do something else */ ()
}
}
A PartialFunction
is a trait that you can implement. You aren't forced to use the case
syntax.
Unfortunately, it doesn't come with a convenient method for composing in the way you describe. The closest is the andThen
method, but the argument you pass must be a regular function, which could lead to match errors when an argument is unhandled in the actual receive function. So you're stuck writing it the long way.
class MessageInterceptor(receiver: Receive) extends Receive {
def apply(msg: Any) = {
/* do whatever things here */
receiver.apply(msg)
}
def isDefinedAt(msg: Any) = receiver.isDefinedAt(msg)
}
val process = new MessageInterceptor(receive)
def process: Receive = printMessage andThen {
case Some(x) => /* do one thing */ ()
case None => /* do another thing */ ()
case _ => /* do something else */ ()
}
def printMessage: PartialFunction[Any, Any] = {
case m =>
println(m)
m
}
I suppose andThen method is a right choice:
def printEverything: PartialFunction[Any, Any] = {
case x =>
println(x)
x
}
and use it:
def receive : Receive = printEverything andThen process
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With