Scala notes

Access sender actor in injected children

 

When you have injected children in an injected parent actor, you might get deadLetters when trying to access the sender from inside the created children.

This is because the sender of the message gets picked up via an implicit parameter.

The signature is:

def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit

The sender gets picked up from this implicit parameter. When sending a message from within the parent actor, this parameter is set to the default value: Actor.noSender which translates to deadLetters when accessing sender from within the children.

In order to have access to the sender inside the child, we can use tell :

final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)

This way, sender can be correctly set and passed to the child actor.