user650228
user650228

Reputation:

Actors case class simple example

I'm almost certainly doing something profoundly stupid which makes this actor not work properly, but I can't see it after a chunk of time staring at it. So I thought I'd ask SO.

I can't get this code to work - in that, if I send it messages such as 4 (which should trigger the default case), nothing is printed, and I can't trip the monitor by sending it a temperature alarm.

What am I doing wrong?

class TemperatureMonitor extends Actor {
  var tripped : Boolean = false
  var tripTemp : Double = 0.0

  def act() {
    while (true) {
      receive {
        case Heartbeat => 0
        case TemperatureAlarm(temp) =>
          tripped = true
          tripTemp = temp
        case _ => println("No match")
      }
    }
  }
}

Upvotes: 1

Views: 289

Answers (1)

Theo
Theo

Reputation: 132902

Since you don't show how you create the actor we can only guess. The first thing I would check is that you've started the actor:

val monitor = new TemperatureMonitor
monitor.start
monitor ! 4 // should trigger the default case, as you say

Upvotes: 5

Related Questions