Search code examples
scalaakkaactor

Is there a way to avoid untyped ActorRef in scala cookbook actor communication example?


In scala cookbook: 13.3. How to Communicate Between Actors I see this

class Ping(pong: ActorRef) extends Actor { // OMG - ActorRef - no type, help!
  var count = 0
  def incrementAndPrint { count += 1; println("ping") }
  def receive = {
    case StartMessage =>
      incrementAndPrint

I have got also a few places in my own code where I have this ActorRef I don't like it as I liked type safety. Is there a way to avoid that in the above pong example?

Side Note: I understand I can use "actorFor" with naming, but as a DI freak I rather pass it in constructor / parameter.


Solution

  • Some stuff is in the works for Akka 3.0 eg see this teaser thread: https://mobile.twitter.com/RayRoestenburg/status/510511346040197120

    There is a pattern for type safety now using a custom ask (the question mark). Here is a blog about it: http://www.warski.org/blog/2013/05/typed-ask-for-akka/

    This is a little clunky though and may not be worth the trouble.

    Another approach is to create typed APIs and wrap your actors in them.