问题
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
- not make the Actor fail with a
MatchError
- 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.
来源:https://stackoverflow.com/questions/41260676/why-is-actor-receive-a-partial-function