1

这是我从书中使用的代码...

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

4

1 回答 1

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:

当我编译它时,我得到以下文件:

  • 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

如果我调用它,scala -cp . ActorTest我会得到:

ricoeur:~ tom$ scala -cp 。演员测试
我的邮箱里有 6 条消息。
我的邮箱里有 5 条消息。
我的邮箱里有 4 条消息。
^C

它会在“我的邮箱中有 4 条消息”输出之后等待,直到我 Ctrl+C 它。

希望有帮助。

于 2010-04-14T15:52:12.707 回答