How to use akk actors in scala

I am relatively new to the idea of ​​actors, and I was interested to know if I can criticize what I do. For part of the project I need an actor who will tell the collective of listening actors time. Performing actors must be added to this actor.

I currently have this:

import akka.actor.Actor;

import akka.actor.ActorRef;
import com.github.nscala_time.time.Imports._;

class TimeManager extends Actor {
  var actors:List[ActorRef] = List();
  def receive = {
    case AdvanceTime() => actors foreach (_ ! DateTime.now)
    case AddListener(x) => actors =  x :: actors
  }
}

Is there any way to remove the state (var actors) from this code to make it more functional?

0
source share
2 answers

You cannot delete a state because it TimeManagermust contain a list of participants.

You can hide this:

class TimeManager extends Actor {
  def receive = getBehavior(Nil)
  def getBehavior(actors: List[ActorRef]): Receive = {
    case AdvanceTime() => actors foreach (_ ! DateTime.now)
    case AddListener(x) => context become getBehavior(x :: actors)
  }
}
+6
source

Scala - OO , Scala var, , Haskell.

, , , , actors private

+1

All Articles