joesan
joesan

Reputation: 15385

Akka context become recursive function

I have an actor in which I mutate the state using context.become: Here is the snippet:

def stateMachine(state: State): Receive = {
  case a => {
    ... do something
    context.become(stateMachine(newState))
  }

  case b => {
    ... do something
    sender ! state
  }

  case c => {
    ... do something
    context.become(stateMachine(newState))
  }
}

My IntelliJ says that my stateMachine(...) function is recursive. Is this a problem? Should I be concerned? Is there something fundamentally wrong with my approach in the above example?

Upvotes: 5

Views: 572

Answers (1)

mattinbits
mattinbits

Reputation: 10428

The approach you are using is fine, it is a common way to implement state inside an Actor without using var. The default version of context.become does not maintain a stack, it just replaces the existing functionality with the new one. The is called "HotSwap". To maintain a stack, you would have to add discardOld = false.

http://doc.akka.io/docs/akka/snapshot/scala/actors.html#become-unbecome

http://doc.akka.io/docs/akka/1.3.1/scala/actors.html#actor-hotswap

Upvotes: 5

Related Questions