Conclusion

In this example, we’ve seen how Transit helps you build type-safe state machines. We started with a simple door that can be open or closed, and learned the core workflow:

  1. Use Variant types for both State and Msg instead of traditional ADTs.

  2. Define the state machine using Transit’s type-level DSL specification

  3. Implement the update function using mkUpdate with match clauses that the compiler verifies against the specification

  4. Generate documentation automatically — both state diagrams and transition tables — from the same specification

The key advantage is that your specification, implementation, and documentation all stay in sync because they share the same source of truth. The compiler ensures your code matches your specification, and your documentation is generated directly from it.

While this example was simple, it demonstrates Transit’s fundamental approach. In the next example, we’ll see how Transit handles more complex scenarios with states that contain data and conditional transitions.

↑ Back to top