Thayne
Thayne

Reputation: 7042

Why isn't ask defined directly on ActorRef for Akka?

I am learning about Akka, and while exploring the API, I came across something kind of curious (at least to me). The tell function is defined directly on the ActorRef class. However, the ask function is declared in the AskSupport trait. I can't think of any good reasons why they needed a seperate trait for AskSupport rather than including ask in the API for ActorRef (and ? in the API of ScalaActorRef). Would anyone care to enlighten me on the reasoning behind this?

Upvotes: 7

Views: 2498

Answers (3)

Rob Napier
Rob Napier

Reputation: 299623

The Actor docs gives the author's reasoning:

The ask pattern involves actors as well as futures, hence it is offered as a use pattern rather than a method on ActorRef

The ask pattern includes Future extensions, not just ActorRef extensions. In particular, if you look through the akka.pattern docs, you'll note PipeToSupport and PipeableFuture beyond the extensions to Akka objects.

Upvotes: 4

cmbaxter
cmbaxter

Reputation: 35463

Keep in mind that Akka is based on the Erlang system of actors and I believe that tell is the only facility to communicate between actors in that system. I imagine that the Akka guys want to keep the ActorRef in Akka as similar as possible to it's Erlang analog.

One other thing to keep in mind is that ask is simply a pattern of using a tell and then setting up a Future and temporary actor to handle the responding tell to make it look like a native request/response process. But under the hood, it's really just two actors telling back and forth. It's a convention of using tells to create the appearance of request/response and that's why it's setup as a pattern in Akka that can be pulled in when needed as opposed to being part of the ActorRef.

Upvotes: 2

Kevin Wright
Kevin Wright

Reputation: 49705

According to the Akka Docs:

There are performance implications of using ask since something needs to keep track of when it times out, there needs to be something that bridges a Promise into an ActorRef and it also needs to be reachable through remoting. So always prefer tell for performance, and only ask if you must.

Given this, it makes sense that you'd want to subtly discourage the use of ask by requiring users to explicitly import the facility. This has the added benefit of reducing bloat in the ActorRef API.

Upvotes: 4

Related Questions