Tim
Tim

Reputation: 2028

Pass generic parameter as agrument

What I want to do is (currently does not compile):

def createSystem(system:ActorSystem, actorMap: Map[String, type]) = {
    for( (name, actor) <- actorMap){
      system.actorOf(Props[type], name) //<- should be called like Props[MyActor1]   
    }
  }

and then call it like:

def standardSystem(system:ActorSystem):Unit = {
    createSystem(
      system,
    Map(
      "actor1" -> classOf[MyActor1],
      "actor2" -> classOf[MyActor2]
    ))
  }

The problem is that Props[T] expects type to be passed in, but I don't know how to pass type in the map, and how to refer to this type in the call to Props.

Upvotes: 0

Views: 128

Answers (2)

user3779650
user3779650

Reputation: 46

Define the map Map[String , Class[_ <: Actor]]

As was answered above use Props.create(actor) and iterate it

for ( (name,clazz) <- actorMap ) {
  system.actorOf(Props.create(clazz) , name )
}

Upvotes: 1

Sean Vieira
Sean Vieira

Reputation: 159955

Akka's Props provides an create method that takes a class. Just call it with your class:

system.actorOf(Props.create(actor), name)

Upvotes: 0

Related Questions