Switching project to typed

I’m switching a small project from untyped actors to typed actors. I’m struggling with the fact that my untyped actors change ("become") actors with a different protocol. With typed actors, onMessage is restricted to produce a Behavior[T] for the same T (as far as I can tell, or a super type of T using narrow). I end up with actors that can only meaningfully handle a subset of their message type, and with actors sharing parts of their protocols. The application works, but uses actors with types too broad for their actual behaviors, and I feel I’m not getting the type safety I would expect from the switch.

I think I would need to see some simplified example or more concrete description to understand the problem.

Have you explored using messageAdapter?

First, I wanted to confirm that an actor cannot become an actor of a different type.

My case goes something like this. An actor receives a request of type Q and must produce a response of type R. For each request, it spawns children that will produce messages of type M. It then becomes an actor that gathers these replies. As such, it does not expect Q messages. Once the replies have been gathered, it “unbecomes” and produces its response R. As such, it does not expect messages of type M. Right now, I have it working by having Q, R and M part of the same protocol, which doesn’t feel right.

I wouldn’t say explored, but I had a quick look (too quick to make sense of it). If that’s the direction I need to go, I’ll read more.

That is right, but that type can be a super type of what is exposed to the outside by the initial Behavior. Taking it to the extreme the internal super type can be Any. The initial Behavior can expose a subtype to the outside by using narrow.

You can use messageAdapter to decouple the protocols for Q and M. The replies from the children would then not have to know about message type of the parent. The parent would adapt the replies from the children into messages that it understands.

However, the child actors in your example are probably rather tightly coupled to the parent anyway so it’s nothing wrong with sharing message types between parent and children here, instead of using messageAdapter.

I think the section Public versus private messages in the style guide should give you some ideas of how to structure it.

messageAdapter is something you will need sooner or later so worth learning Adapted Response

1 Like