title: How to check an Actor/ActorRef's type when injecting dependencies?
body: <p>Is there any pattern for type checking an <code>ActorRef</code> in Akka (scala)? When I pass a reference to another actor, I want to make sure it's of the correct type. However, when getting a reference, using the <code>context</code> of an actor, it always returns <code>ActorRef</code>, so I can't really use sub-classing to check this.</p>
  
  <p>A concrete example follows. In Akka, I inject dependencies by passing <code>ActorRef</code> into the constructor of another <code>Actor</code>. Look at the contrived example below: <code>Speaker</code> is constructed with a reference to <code>Listener</code>. I create a props constructor in the companion object, as encouraged by the Akka documentation.</p>
  
  <pre class="lang-scala prettyprint-override"><code>package test
  import akka.actor._
  
  object scratchpad {
     object Speaker {
       def apply(listener : ActorRef) : Props = Props(classOf[Speaker],listener)
     }
  
     class Speaker private(listener : ActorRef) extends Actor {
           override def preStart() = listener ! "knock knock"
     }
  
     class Listener extends Actor {
       def receive : Receive = {
         case "knock knock" =&gt;
           //received message
       }
     }
  
     val system = ActorSystem("test")
     val listener = system.actorOf(Props[Listener])
     val speaker = system.actorOf(Speaker(listener))
  }
  </code></pre>
  
  <p>The problem is that I could create another actor, say <code>DeafMute</code>, and pass this in instead - but the compiler wouldn't catch this, because it's also of type <code>ActorRef</code>. Is there a pattern to work around this?</p>