I've started playing with Akka and have found that most of my actors have part immutable state and part mutable state. Both could be merged into a State
case class which could then be copied on solely its mutable state and passed back into apply
to update the Behavior
.
However, it'd be magical if that wasn't necessary. Is it possible for a partially applied Scala function to somehow recursively call itself, but starting at its 2nd parameter list? Rather than starting the entire chain from the beginning?
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)
}
Ah, maybe I was looking for solutions in the wrong area. A nested function should do the trick actually!
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)
}