Why is Actor.receive a partial function?

2019-07-04 22:53发布

问题:

Why is Actor.receive a partial function? I can always have a regular function with match expression instead of it.

回答1:

It is a PartialFunction to capture the possibility of a message being handled or unhandled by the Actor. An unhandled message will

  1. not make the Actor fail with a MatchError
  2. spawn an UnhandledMessage event to the event stream

More info here.



回答2:

One reason would be that the partial function has isDefinedAt method, which allows Akka to check if the message can be handled without catching the scala.MatchError exception from user's code.



标签: scala akka