title: Akka Actor ask and Type Safety
body: <h2>How can I use Akka Actor ask and maintain type safety? or avoid using ask in favour of tells?</h2>
  
  <p>When calling <code>?</code> or <code>ask</code> on an Akka Actor, a <code>Future[Any]</code> is returned and I have to do an explicit cast via <code>future.mapTo[MyType]</code>.</p>
  
  <p>I don't like losing this type safety. If I use Futures directly (with no actors) I can explicitly return <code>Future[MyType]</code> and maintain type safety.</p>
  
  <p><em>My specific use case involves an actor delegating it's message to two child actors and then aggregating the results from those actors and returning that to the parent's sender. My parent's receive method looks similar to this approach in the Akka Docs:</em></p>
  
  <p><a href="http://doc.akka.io/docs/akka/2.0/scala/futures.html#For_Comprehensions" rel="noreferrer">http://doc.akka.io/docs/akka/2.0/scala/futures.html#For_Comprehensions</a></p>
  
  <pre><code>val f1 = actor1 ? msg
  val f2 = actor2 ? msg
  
  val f3 = for {
    a ← f1.mapTo[Int]
    b ← f2.mapTo[Int]
    c ← ask(actor3, (a + b)).mapTo[Int]
  } yield c
  </code></pre>
  
  <p>Is there a better way to achieve my use case?</p>