4 ms·
Many popular computer languages in use make state machines unergonomic. The problem is solved with native enumerated types or, even better, tagged unions, like
by giovannibonetti 4y ago
Many popular computer languages in use make state machines unergonomic. The problem is solved with native enumerated types or, even better, tagged unions, like Rust and functional languages from the ML family (Haskell, Ocaml, Elm). It is a joy working with state machines when you have a compiler that can verify exhaustively wheter all states and state transitions are handled.
- cal85 4y agoI've only really used state machines in TypeScript via xstate, and I've been meaning to try Rust for a while, so your comment interests me. Got any tips or advice on further reading or specific libs that you can recommend for working with FSMs in Rust?
- giovannibonetti 4y agoI don't have much Rust-specific experience, but I wonder if you need a library for this when the language/compiler is doing all the heavy lifting. When working with Elm, for example, that uses that a lot for the Model-View-Update framework, you just define your model states and pattern match them to build the view and update functions.
- giraffe_lady 4y agoLook at how you'd do it in f#, ocaml, or elm first imo. The type systems of those languages allow for a very tight approach with no confounding factors. The way rust does it is the same, but with more details leaked in from the constraints rust imposes. Which is fine, but I think it's valuable to see the "pure" stripped back form in a language almost perfectly suited to it first.
- orwin 4y agoSymbolic languages make it trivial. The only really complex state machine i ever wrote was in lisp (it was an IA for a game). Not the most intelligent IA, but a breath to write. The only feature a language really need to make usable state machines are closures.
- andrewjl 4y agoThere are some interesting approaches in Swift: https://github.com/apple/swift/blob/main/stdlib/public/Observation/Sources/Observation/ObservationRegistrarStateMachine.swift https://github.com/apple/swift/blob/main/stdlib/public/Obser... https://github.com/apple/swift/blob/main/stdlib/public/Observation/Sources/Observation/ObservationRegistrar.swift https://github.com/apple/swift/blob/main/stdlib/public/Obser...