我become
在我的 Akka 演员中使用时遇到了一些麻烦。基本上,我的演员有这样的结构:
// This is where I store information received by the actor
// In my real application it has more fields, though.
case class Information(list:List[AnyRef]) {
def received(x:AnyRef) = {
Information(list :+ x)
}
}
class MyActor extends Actor {
// Initial receive block that simply waits for a "start" signal
def receive = {
case Start => {
become(waiting(Information(List())))
}
}
// The main waiting state. In my real application, I have multiple of
// these which all have a parameter of type "Information"
def waiting(info:Information):Receive = {
// If a certain amount of messages was received, I decide what action
// to take next.
if(someCondition) {
decideNextState(x)
}
return {
case Bar(x) => {
//
// !!! Problem occurs here !!!
//
// This is where the problem occurs, apparently. After a decision has been
// made, (i.e. decideNextState was invoked), the info list should've been
// cleared. But when I check the size of the info list here, after a decision
// has been made, it appears to still contain all the messages received
// earlier.
//
become(waiting(info received x))
}
}
}
def decideNextState(info:Information) {
// Some logic, then the received information list is cleared and
// we enter a new state.
become(waiting((Information(List())))
}
}
很抱歉代码片段太长,但我真的不能让它更小。
出现问题的部分在评论中标注。我将参数传递给返回Receive
部分函数的方法,然后将其传递给该become
方法。但是,创建的部分函数似乎以某种方式保留了早期调用的状态。我发现这个问题有点难以解释,但我已尽力在代码中的注释中这样做,所以请阅读这些,我会回答任何不清楚的地方。