CPS
CPS

Reputation: 537

Value ! is not a member of Actor

I've got an Actor class that manages a list of Actors. When it receives a message of a certain type, it passes it off to each of the Actors it knows about.

 var loggers : List[Logger]    

def receive = {
  ...
  // log request
  case logmessage : LogMessage => {
      // send message to each logger (if none, nothing happens)
      for (logger <- loggers)
        logger ! logmessage
  }
  ...     
}

I'm getting a compile error on logger ! logmessage : "value ! is not a member of (package).Logger". The ! makes this very difficult to google. The Logger class compiles, having its own receive method, one line including self ! PoisonPill, meaning the ! operator works there. They're in the same package.

Upvotes: 4

Views: 3160

Answers (1)

Dylan
Dylan

Reputation: 13859

I'm assuming Logger extends Actor

In Akka, message sending is done between ActorRefs, not Actors. Every actor's self is an ActorRef instance. So you would want to change your loggers var to be var loggers: List[ActorRef].

Better yet, you could use an ActorSelection, which has a ! method that sends the message to all actor refs in the selection. You can find methods for creating an ActorSelection in the Actor's context.

P.S. You can use the "#" page on the Akka Scaladocs to find methods that start with symbols. On that page you can see that there is a ! defined on ScalaActorRef and ScalaActorSelection, both of which are implicit upgrades from ActorRef and ActorSelection.

Upvotes: 11

Related Questions