Currently there are two documented styles for handling private actor messages:
Extend public base trait with private classes:
sealed trait Command
case class PublicMessage() extends Command
private case class PrivateMessage() extends Command
and make separate base trait, and make public messages extend that trait:
sealed trait Message
sealed trait Command extends Message
private sealed trait PrivateMessage extends Message
Personally, I don’t like both of them because they put public interface and private implementation details together in same place, thus making it less readable and clobbering it. Even if you have 5 public message types and 5 private ones, the definition becomes rather “noisy”. I think that it’s better to keep them separated. So I propose third one, which achieves this goal, but isn’t possible with current Akka since Akka is missing one small thing:
object Actor {
sealed trait Command
...
def apply(...): Behavior[Command] = ...
}
private object ActorImpl {
sealed trait Message
case class CommandMessage(cmd: Command) extends Message
...
}
private class ActorImpl[Message] extends AbstractBehavior[Message] ...
This way primary Actor
object contains only public messages and factory method, and implementation details can be hidden in separate file.
And here comes the main point: there is no something like .map
on behavior.
You can’t create abovementioned pattern since you’ll end up with Behavior[Message]
instead of Behavior[Command]
, and .narrow
will not help in this place. Method like
trait Behavior[T] {
def contramap[U](f: U => T): Behavior[U]
}
would be exteremly useful in such situation.
So the whole point of this topic: what do you think about above pattern and is contramap
feature feasible for Github feature request? It seems like rather simple and obvious feature, and propably there were reasons for not including it.