I m new to scala. When learning Actor, I tried to extend it to save one line of def:
import scala.actors.Actor
import Actor._
class Actoo(actoo: =>Unit) extends Actor {
def act() {actoo}
}
object run extends Application {
/*
// this one runs well
val a = new Actor {
def act() {
receive { case 1 => println("1") }
}
}
*/
val a = new Actoo {
receive { case 1 => println("1") }
}
a.start
a ! 1
}
Then the exception trace looks like this:
java.lang.AssertionError: assertion failed: receive from channel belonging to other actor
at scala.Predef$.assert(Predef.scala:92)
at scala.actors.Actor$class.receive(Actor.scala:424)
at Actoo.receive(actoo.scala:3)
at run$$anon$1.<init>(actoo.scala:16)
at run$.<init>(actoo.scala:15)
at run$.<clinit>(actoo.scala)
at run.main(actoo.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
There may be many alternatives which can do the same thing, but it's better for me to know the reason why the code above doesn't work. thx ~