What is a difference between `receivePartial` and `receive`?

I would like to understand the details between them

This topic is based on a conversation between an anonymous user of Akka and our “Ask AI about Akka” bot in the documentation.

In Akka, both receive and receivePartial are used to define the behavior of an actor in response to incoming messages. However, they differ in how they handle messages.

receive is a method that takes a total function as an argument. This function should cover all possible incoming messages that an actor can handle. If a message is not handled, a MatchError will be thrown at runtime. This is why it’s recommended to use a sealed trait as the super type of the commands (incoming messages) of an actor, as the compiler will emit a warning if a message type is forgotten in the pattern match. If a Behavior should not handle certain messages, you can still include them in the pattern match and return Behaviors.unhandled.

val zero: Behavior[Command] = {
  Behaviors.receiveMessage {
    case GetValue(replyTo) =>
      replyTo ! Value(0)
      Behaviors.same
    case Down =>
      Behaviors.unhandled
  }
}

(Source: Akka Docs)

On the other hand, receivePartial is a method that takes a partial function as an argument. This function only needs to cover the messages that the actor can handle. If a message is not covered by the function, it’s considered as unhandled. This can be useful to cover potential future unhandled message cases.

val zero: Behavior[Command] = {
  Behaviors.receiveMessagePartial {
    case GetValue(replyTo) =>
      replyTo ! Value(0)
      Behaviors.same
  }
}

(Source: Akka Docs)

In summary, receive requires you to handle all possible messages (or explicitly mark them as unhandled), while receivePartial allows you to only handle a subset of possible messages.