This is the code I used from a book...
import scala.actors.Actor._
val countActor = actor {
loop 开发者_如何学运维{
react {
case "how many?" => {
println("I've got " + mailboxSize.toString + " messages in my mailbox.")
}
}
}
}
countActor ! 1
countActor ! 2
countActor ! 3
countActor ! "how many?"
countActor ! "how many?"
countActor ! 4
countActor ! "how many?"
The error
java.lang.NoClassDefFoundError: Main$$anon$1$$anonfun$1$$anonfun$apply$2
I'm guessing you are executing with just scala rather than compiling. The script does work if you compile it (and wrap it in an Application-trait singleton object):
import scala.actors.Actor._
object ActorTest extends Application {
val countActor = actor {
loop {
react {
case "how many?" => { println("I've got " + mailboxSize.toString + " messages in my mailbox.") }
}
}
}
countActor ! 1
countActor ! 2
countActor ! 3
countActor ! "how many?"
countActor ! "how many?"
countActor ! 4
countActor ! "how many?"
}
// vim: set ts=4 sw=4 et:
When I compile that, I get the following files:
- ActorTest$$anonfun$1$$anonfun$apply$2$$anonfun$apply$1.class
- ActorTest$$anonfun$1$$anonfun$apply$2.class
- ActorTest$$anonfun$1.class
- ActorTest$.class
- ActorTest.class
If I invoke it with scala -cp . ActorTest I get this:
ricoeur:~ tom$ scala -cp . ActorTest I've got 6 messages in my mailbox. I've got 5 messages in my mailbox. I've got 4 messages in my mailbox. ^C
It sits and waits after the "I've got 4 messages in my mailbox" output until I Ctrl+C it.
Hope that helps.
精彩评论