这是我在书中使用的代码......
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?"
java.lang.NoClassDefFoundError:Main $$ anon $ 1 $$ anonfun $ 1 $$ anonfun $ apply $ 2
答案 0 :(得分:3)
我猜你是用scala执行而不是编译。如果编译它(并将其包装在Application-trait单例对象中),该脚本可以正常工作:
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:
编译时,我得到以下文件:
如果我使用 scala -cp调用它。 ActorTest 我明白了:
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
在“我的邮箱中有4封邮件”输出后,它会等待,直到我按Ctrl + C它。
希望有所帮助。