Type-safe finite state machines in zig!

I used to develop type-safe finite state machines in haskell.https://github.com/sdzx-1/typed-fsm

The same effect can be achieved by relying on some programming conventions in zig.

I made some examples using typed-fsm-zig: GitHub - sdzx-1/typed-fsm-zig: typed-fsm-zig

typed-fsm + gui is a very good choice!!!

6 Likes

The advantages described here also exist in zig!

Added more introduction!

5 Likes

The latest typed-fsm-zig simplifies the state and allows state transition functions based on state transition messages, which is a victory for abstraction.

code

6 Likes

Hello,

is there a way to have the GUI loop managed externally from the state machine logic?

From what I understand, your FSM can’t retain static typing unless genMsg runs the gui loop to retrieve the user input.
But, there are situations where the GUI can’t be run directly from genMsg(), for example if it’s managed by a host application.
I’ve looked into it, and I can’t find a solution: no matter what, if genMsg doesn’t own the loop, then you have to resort to using a dispatcher function, which nullifies the strong typing…

Any hints?

1 Like

This is really cool. Thanks for sharing.

I wonder if you’ve seen the CREM module?

IMO, it has some pretty interesting ideas that go a long way toward making illegal states materialize as type errors. It uses Singletons to do much of that. I wonder is that plausible or possible in Zig?

Another cool feature is being able to generate a diagram of all possible states and transitions solely using the code.