I have a scenario where I have some objects that need to take in references from each other. The only way I can get this to compile is to use lazy
class A(b:B)
class B(a:A)
lazy val a:A = new A(b)
lazy val b:B = new B(a)
I can do the same thing using some actors, and get it to compile also
abstract class Message
case class Message1 extends Message
case class Message2 extends Message
class Actor1(otherActor:Actor) extends Actor {
def act() {
loop {
react {
case Message1 =>
println("received message1")
otherActor ! Message2
case _ =>
}
}
}
}
class Actor2(otherActor:Actor) extends Actor {
def act() {
loop {
react {
case Message2 =>
println("received message2")
otherActor ! Message1
case _ =>
}
}
}
}
lazy val actor1:Actor = new Actor1(actor2)
lazy val actor2:Actor = new Actor2(actor1)
However, when I add the following:
actor1.start
actor2.start
actor1 ! Message1
I get the following error:
Exception in thread “main”
java.lang.NoClassDefFoundError:
com/fictitiousCompany/stackOverflowQuestion/Test
Caused by:
java.lang.ClassNotFoundException:
com.fictitiousCompany.stackOverflowQuestion.Test
at
java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at
java.security.AccessController.doPrivileged(Native
Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:248)
I’m using the Scala Eclipse Plugin 2.8.1.
Note that even your smaller example would have issues (in the REPL):
As soon as
aneeds to be evaluated therefore constructed, it would require B, which requires A. In order for this to workaorbwould have to finish being constructed.Using by-name parameters allows the smaller example to evaluate.
Note sure if that’ll work for your actor example as well.
Edit: by name params worked locally on 2.8.0. I replaced case class with object to get rid of some deprecation warnings and added start methods on actor1, actor2 and kick the whole thing with
actor1 ! Message1. Aside from this I haven’t used actor before, so I can’t comment more. Here is what I tested:Prints a bunch of: