1

I need to create an actor depending on command line parameters:

def main(args: Array[String]): Unit = {
  val t = args(0) match {
    // 1
    case 1 => // MyActor1 
    case 2 => // MyActor2
    case 3 => // MyActor3
  }

  // 2
  val app = system actorOf (Props[/* ??? MyActor1(2,3).type? - not working*/], "app")
  app ! "Start"
}

abstract class MyActorBase extends Actor {...}
class MyActor1 extends MyActorBase {...}
class MyActor2 extends MyActorBase {...}
class MyActor3 extends MyActorBase {...}

So I have 2 questions: what should be returned by match and what should I pass to Props?

In C# I would use typeof operator, however, the following code doesn't work in Scala MyActor1.type

Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • The fundamental question being asked is impossible. `Props[X]` is something that is evaluated at _compile time_. At run-time, that doesn't exist (it has been replaced by the result of the evaluation), so it cannot be done dynamically. – Daniel C. Sobral Dec 11 '13 at 16:58

1 Answers1

4

Just a matter of proper keywords (here I'm assuming you're using latest version of Props api):

val actorClass = args(0) match {
   case 1 => classOf[MyActor1]
   case 2 => classOf[MyActor2]
}
val app = system actorOf (Props(actorClass), "app")
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • If I say: `val actorClass: Class[MyActorBase] = args(0)...` then it says "type mismatch". I wonder why? – Incerteza Dec 11 '13 at 16:11
  • 1
    @Alex `Class` is a Java class and, therefore, invariant. Being invariant means that `Class[A] <: Class[B]` is only true if `A` and `B` are the same type. In all other cases, including `A <: B` (but different), that relation is false. – Daniel C. Sobral Dec 11 '13 at 16:56
  • @DanielC.Sobral, what's invariant? – Incerteza Dec 11 '13 at 18:33
  • @Alex I just told you want being invariant means. You can find more information in [this question](http://stackoverflow.com/q/8481301/53013). – Daniel C. Sobral Dec 11 '13 at 18:47