Relevance of FSMs with Akka Typed

Hi all!

I am a bit confused about how and why FSMs are considered irrelevant with Akka Typed. What I mean is the first statement on the page Behaviors as Finite state machines (https://doc.akka.io/docs/akka/current/typed/fsm.html): “No support is needed in Akka Typed as it is straightforward to represent FSMs with behaviors.”

While it may be technically true that this is the case, the FSM DSL actually added so much readability that I am already grieving for it. Another point, which is less aesthetical than functional and which makes me doubt if all untyped FSM use cases can be realized using Behaviors, is how Behaviours are supposed to replace onTransition? The webpage says that “Any side effects that were previously done in a onTransition block go directly into the behaviors.” How is this possible? I mean, how can I specify logic that should be executed between two specific behaviors?

Best,
Michael

My take after a quick look at the examples.
When you use the untyped FSM, you declare the possible states as a sealed trait hierarchy.

That means that state-switching cannot be used to pass directly data with the transition (you only have goto(newState), so the data is the target state value).
Therefore you need to pass the additional data out-of-band via some event that needs be captured by onTransition.

When you use typed, you switch state by defining the next behaviour, which can be defined as a value (i.e. a constant function) or as a method taking parameters.
So the extra information needed to switch FSM state is encoded directly in how behaviours are defined.

Akka-typed effectively adds parametric states, hence all information is passed directly via the returned behaviour of each message handling.

1 Like

Hi Ivano, thanks for your response. I think in the meantime I have gained a better understanding of Akka Typed. Before, I had felt that I had more flexibility with untyped FSMs when implementing state entry and exit actions but after playing through a few scenarios I think now that Behaviors are in fact a much more powerful concept and that it’s easier to leverage the strengths of a functional language such as Scala. So the only thing I’m gonna miss is the nice FSM DSL but that’s probably something I will survive ;-)

1 Like

I guess you can try to write your own on top of the typed behaviours. It should be relatively easy, given the existing api… :wink:

1 Like