Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Fixing the first sample you provided, see the comments:</p> <pre><code>classnames = List[String]("fqcn", "fqcn", "fqcn"....) // close ( with ) not ] for (name &lt;- classnames) { val clazz = classLoader.forName(name) // forName is what loads a class //val actRef = actorSystem.actorOf(Props[clazz]) // still wrong I will explain later } </code></pre> <p>The second sample has a few more issues.</p> <p>First <code>HashMap([String, String, ActorRef]("commonname" -&gt; "Fqcn", someThing))</code>, it's close to be right but with a few key syntactic mistakes. </p> <p>In Scala the <code>[]</code> is used to declare or define type parameters, it seems our case in hand is a matter of definition, in other words, we want to state what type paramaters. There are only a few places Scala allows placing type parameters, it's right classes after class instanciation (e.g <code>new HashMap[String, Int]</code>) and in method call (e.g <code>list.map[String](...)</code>.</p> <p>So, placing the type parameter as in <code>HashMap[String, String, ActorRef](...)</code> would be valid Scala syntax and since HashMap is companion object it would end up after desugaring something like <code>HashMap.apply[String, String, ActorRef](...)</code>.</p> <p>But this expression still have a big problem, HashMap only take two type parameters, one for the keys and one for the values. Looking at your code it seems that the desired key is a tuple. So to make it work: <code>HashMap[(String, String), ActorRef](...)</code></p> <p>Now for the last fix <code>Props[new classobject]</code>, again confusion regarding type parameters. Type parameters takes <strong>types</strong> not instances, any 'new something' would be creating a instance of something. You have to do <code>Props[SomeClass]</code>. As a quick side not the term "class object" usually refers to instance of a class, not the class itself. </p> <p>A syntactic valid version of the second sample:</p> <pre><code>val activeClasses = HashMap[(String, String), ActorRef]( ("commonname" -&gt; "Fqcn", actorOf(Props[SomeClass])) ) </code></pre> <p>Coming back to the first sample and this particular line:</p> <pre><code>val actRef = actorSystem.actorOf(Props[clazz]) </code></pre> <p><code>clazz</code> here is a instance of Class[_], again the above wouldn't even be valid code. It seems that <code>Props.apply[T &lt;: ActorRef]...</code> requires a type and it will take care of instantiating it for you. But you only have Class[_]'s containing the classes. How to solve this? It happens there is another Props.apply that instead takes constructs for the actor classes that will take of the instantiation, look it's signature:</p> <pre><code>def apply(creator: ⇒ Actor) </code></pre> <p>Why not use this one?</p> <pre><code>classnames = List[String]("fqcn", "fqcn", "fqcn"....) for (name &lt;- classnames) { val clazz = classLoader.forName(name) val actRef = actorSystem.actorOf(Props( clazz.newInstance.asInstanceOf[Actor] )) } </code></pre> <p>Assuming the second sample will somehow rely classes from the first:</p> <pre><code>val activeClasses = HashMap[(String, String), ActorRef]( ("commonname" -&gt; "Fqcn", actorOf(Props( clazz.newInstance.asInstanceOf[Actor] ) ) ) </code></pre> <p>A few comments about these samples:</p> <ul> <li>Both type parameters are unnecessary. The compiler can infer then for you.</li> <li>Instead of using <code>(a -&gt; b, c)</code> for defining a tuple whose first element is a tuple, you can use <code>a -&gt; b -&gt;</code>, which will result into <code>((a,b),c)</code>. The <code>-&gt;</code> is left associative.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload