How to return a value of specific type?

I have a Manager class that supports different Actor commands and is the entry point for my Actor system. It routes the messages to the appropriate Worker through a Router. The main method calls the Manager through Ask pattern.

The worker wants to return a value of a specific type. Main shall use this value subsequently.

One way to accomplish is to define a new message in the Manager class and the Worker passes the value to the Manager through this new message. This approach involves a lot of message exchange compared to the traditional procedural approach.

Please suggest to me any other way to accomplish this.

In a pure actor model, all state transfer is through message passing. Anything else (e.g. updating some shared state) outside the actor model and means that the guarantees of the actor model don’t apply and this introduces coupling.

What is the current replyTo in your ask? Is it possible to incorporate the specific type into the reply. Note that if the replyTo is ActorRef<Done>, that can likely be changed to ActorRef<Optional<SpecificType>>, where an empty Optional is “done, but no specific result value”.

Thank you Levi.

I am using replyTo of type ActorRef<specificType>. As a result, Ask returns CompletionStage<specificType>.

I am following the Actor design principle “all state transfer is through message passing”. I pass on local variable only as specificType value through TELL in response to ASK request (local variable has method scope).

What is ActorRef<Done>?

If the replyTo is ActorRef<SpecificType>, as long as the manager passes that replyTo to the worker, the worker can just replyTo.tell(...) with the specific type. There’s no requirement that the reply has to be from the actor the ask was sent to: that actor is free to delegate that responsibility to another actor. Consider a manager of a team who receives a request for some information which the manager doesn’t know: they may send a message to a member of the team who they think has the information saying “can you reply?”, at which point it’s OK if that member replies directly to the requestor rather than reply to the manager who then rephrases the reply and sends the rephrased reply to the requestor.

Done is essentially a standard message type from Akka signifying “something (context-dependent) happened” (e.g. “that thing you asked for has been done”).