scalaakkaakka-actorakka-typed

How do I convert a classic ActorContext to a typed one


Say I define some messages:

sealed trait Command
case class A(i: Int) extends Command
case class B(str: String) extends Command

And then a classic actor as below to handle these messages. On creation I need access to the ActorContext but as a typed context not a classic

class MyActor extends Actor {

  val typedContext: ActorContext[Command] = ???

  def receive = {
    case A(i) =>
      // Do something with i
    case B(str)
     // Do something with str

  }
}

I know I can do self.toTyped[Command] to get the typed self reference. But I cannot find anything similar for the ActorContext. How would I go about converting?


Solution

  • There is no conversion from a classic ActorContext to a typed ActorContext. About the only things a typed ActorContext can do that a classic ActorContext can't are:

    For the last one, you can

    import akka.actor.typed.scaladsl.adapter.ClassicActorContextOps
    

    which will add

    methods which handle typed.