A partial function is defined as
It is a unary function which defines a domain. Not all values of A would be a part of the domain. For instance, in the following code block
The odd values of A would not match the domain. We can check this by the abstract method isDefined which is available for partial functions.
abstract def isDefinedAt(x: A): Boolean
Hence the above defined partial function would match for even values and not for the odd values. Something like the following calls would result in the expected output
Interestingly, PartialFunction offers a method called orElse which allows us to combine domains.
Hence, in the above scenario, if we have an alternate isOdd PartialFunction defined like this
Then we can combine the 2 domains together with an orElse to get something like this
Ok, so far so good. Now how do we take it to the actors. Simple, the Actor.Receive is a PartialFunction.
So let us assume that we want to modularize code now on the basis of infrastructure events and our application events.
So say we define one receive block like this
and another one like this
and now we combine the 2 domains so that our actor is able to deal with both kinds of incoming events
This way, we would be able to keep our receive blocks modularized and still be able to handle all the messages by combining the domains together.