Writing a state machine in Zig

This article was orginally intended to be about Python’s async def, but someone else has already written about it.

According to me, async function coloring that rewrites to state machines is the worst language feature I know of. JavaScript’s async is justifiable, since it uses callbacks instead.

A Linux process is already async, when it makes syscalls.

So how would you write a state machine in Zig?

pub const FooMachine = opaque {
  pub const Start = struct { u32 };
  pub const End = struct { u32 };
  pub fn to_end(x: Start) End { return .{ x[0] }; }
};

Q: Why not use a sum type?
A: Then the different states would be type erased.

Q: This doesn’t look like a state machine.
A: If you want a sealed state machine, use Java.

Q: What if I want to do async I/O in Zig?
A: Use ZIO.