Fully working example:
import akka.actor.{ActorSystem, Props, Actor, ActorRef}
import scala.reflect.ClassTag
object TestActor extends App {
val system: ActorSystem = ActorSystem("rest-service-actor-system")
def makeActor[T <: Actor : ClassTag](id: Int): ActorRef =
system.actorOf(Props(implicitly[ClassTag[T]].runtimeClass), "entity" + id)
class A extends Actor {
override def receive = {
case anything => println("A GOT MESSAGE: " + anything)
}
}
class B extends Actor {
override def receive = {
case anything => println("B GOT MESSAGE: " + anything)
}
}
makeActor[A](1) ! "hello"
makeActor[A](2) ! "bye"
system.shutdown()
}
usually prints:
A GOT MESSAGE: bye
A GOT MESSAGE: hello
It forces you to have a type that is also an Actor
. For instance this code will not compile:
class C
makeActor[C](3)
error you get is:
type arguments [TestActor.C] do not conform to method makeActor's type parameter bounds [T <: akka.actor.Actor]
makeActor[C](3)
^