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?

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! After reading through your sources I realized I forgot to add the case objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.