Definitely not the first person to think of this. But if enums encapsulate state. And all state machines do is describe transitions between states, then if we could specialize on enum members in fn signatures then that would be enough to implement state machines concisely.
-
-
Show this thread
-
[usual disclaimer I'm not a lang designer, nor on any lang team -- take w/ salt] What I'm thinking is that both examples below are functionally equivalent. I suspect that if we could encode state transitions onto enums it would feel more natural -- e.g. lean into &mut selfpic.twitter.com/5tf49IdDct
Show this thread -
Oh lol; the second example doesn't have enough information to track what it transitions into. The signature implies this is valid: if x { *self = Self::A } else { *self = Self::B } The point is that this can't happen. Either compiler should infer, or syntax is needed. Oops.
Show this thread -
Yeah maybe the answer is that it only works for Self::A -> Self::B, taking ownership of Self each time. I mean, it's not perfect but it would remove a lot of boilerplate when trying to store the states inside another struct (something the first example doesn't even try).
Show this thread
End of conversation
New conversation -
-
-
That sounds a little bit like GADT. I believe it's possible to implement a GADT in Rust, it just doesn't look pretty.
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.