无法在 Scala 中编译基本 actor 示例

发布于 2024-10-28 08:27:03 字数 334 浏览 0 评论 0原文

我正在尝试编译官方指南中的示例,即带有乒乓球的示例。 我已将 Ping 和 Pong 类放在它们自己的文件中,即默认包中。然而,Ping 类有编译错误,说找不到 Pong 类,反之亦然。我还尝试清理该项目以便进行重建,但我无法取得任何进展。我正在使用 2.8.1 的最终版本,来自 这里

我做错了什么?

I'm trying to compile the example from the official guide, the one with the ping pong.
I've put the Ping and Pong classes in their own files, in the default package. However, the Ping class has compilation errors, saying it cannot find the Pong class, and vice versa. I also tried to clean the project so that a rebuild will happen, but I cannot make any progresses. I am using the final version of 2.8.1, from here.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会傲 2024-11-04 08:27:03

pingpong.scala 的完整源代码可以在 scala-2.8.1.final-sources.tgz

tgz 中的位置:scala-2.8.1.final-sources/docs/examples/actors/pingpong.scala

该示例假设所有类都在同一个中文件并可以使用以下命令进行编译

scalac pingpong.scala

但如果您想将它们放在单独的文件中:

Ping.scala

import scala.actors.Actor
import scala.actors.Actor._

case object Ping
class Ping(count: Int, pong: Actor) extends Actor {
  def act() {
    var pingsLeft = count - 1
    pong ! Ping
    while (true) {
      receive {
        case Pong =>
          if (pingsLeft % 1000 == 0)
            Console.println("Ping: pong")
          if (pingsLeft > 0) {
            pong ! Ping
            pingsLeft -= 1
          } else {
            Console.println("Ping: stop")
            pong ! Stop
            exit()
          }
      }
    }
  }
}

Pong.scala

import scala.actors.Actor
import scala.actors.Actor._

case object Pong
class Pong extends Actor {
  def act() {
    var pongCount = 0
    while (true) {
      receive {
        case Ping =>
          if (pongCount % 1000 == 0)
            Console.println("Pong: ping "+pongCount)
          sender ! Pong
          pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong: stop")
          exit()
      }
    }
  }
}

pingpong.scala

case object Stop

object pingpong extends Application {
  val pong = new Pong
  val ping = new Ping(100000, pong)
  ping.start
  pong.start
}

然后运行 ​​scalac *.scala

The full source for pingpong.scala can be found in scala-2.8.1.final-sources.tgz

Location in tgz: scala-2.8.1.final-sources/docs/examples/actors/pingpong.scala

The example assumes all the classes are in the same file and can be compiled with

scalac pingpong.scala

But if you wanted to put them in separate files:

Ping.scala

import scala.actors.Actor
import scala.actors.Actor._

case object Ping
class Ping(count: Int, pong: Actor) extends Actor {
  def act() {
    var pingsLeft = count - 1
    pong ! Ping
    while (true) {
      receive {
        case Pong =>
          if (pingsLeft % 1000 == 0)
            Console.println("Ping: pong")
          if (pingsLeft > 0) {
            pong ! Ping
            pingsLeft -= 1
          } else {
            Console.println("Ping: stop")
            pong ! Stop
            exit()
          }
      }
    }
  }
}

Pong.scala

import scala.actors.Actor
import scala.actors.Actor._

case object Pong
class Pong extends Actor {
  def act() {
    var pongCount = 0
    while (true) {
      receive {
        case Ping =>
          if (pongCount % 1000 == 0)
            Console.println("Pong: ping "+pongCount)
          sender ! Pong
          pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong: stop")
          exit()
      }
    }
  }
}

pingpong.scala

case object Stop

object pingpong extends Application {
  val pong = new Pong
  val ping = new Ping(100000, pong)
  ping.start
  pong.start
}

and then run scalac *.scala

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文