我开始与Akka一起玩,发现我的大多数演员都具有部分不变的状态和部分可变的状态。两者都可以合并为State
案例类,然后可以仅在其可变状态下复制它们,然后传递回apply
以更新Behavior
。
但是,如果没有必要,那将是神奇的。是否可以部分应用Scala函数以某种方式递归调用自身,但从其第二个参数列表开始?而不是从头开始整个链?
sealed trait Command
final case class AddA() extends Command
final case class AddB() extends Command
def apply(
immutableState1: String,
immutableState2: String,
immutableState3: String
)(
mutableState: List[String] = Nil
): Behavior[Command] = Behaviors.receiveMessage {
// without respecifying all immutable state:
case AddA() => CallIts2ndParamList("A" :: mutableState)
// what I'm trying to avoid:
case AddB() => apply(
immutableState1,
immutableState2,
immutableState3
)("B" :: mutableState)
}
啊,也许我在错误的地方寻找解决方案。嵌套函数实际上应该可以解决问题!
sealed trait Command
final case class AddA() extends Command
final case class AddB() extends Command
def apply(
immutableState1: String,
immutableState2: String,
immutableState3: String
): Behavior[Command] = {
def nestedApply(mutableState: List[String]): Behavior[Command] =
Behaviors.receiveMessage {
case AddA() => nestedApply("A" :: mutableState)
}
nestedApply(Nil)
}