Akka 上下文成为递归函数 [英] Akka context become recursive function
问题描述
我有一个演员,我在其中使用 context.become 改变状态:这是片段:
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))
}
}
我的 IntelliJ 说我的 stateMachine(...) 函数是递归的.这是一个问题吗?我应该担心吗?在上面的例子中,我的方法有什么根本性的错误吗?
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?
推荐答案
您使用的方法很好,这是在不使用 var 的情况下在 Actor 内部实现状态的常用方法.context.become
的默认版本不维护堆栈,它只是用新功能替换现有功能.这被称为热交换".要维护堆栈,您必须添加 discardOld = false
.
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
这篇关于Akka 上下文成为递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!