Functional State Machines in Rust: Typestate and Newtype Patterns (dl.acm.org)
67 points by matt_d 10 hours ago
vatsachak 7 hours ago
Types are puzzles. A good Rustacean will make sure that the pieces fit to make the picture.
That's why in crates where I need to make sure certain functions are called in order, I use a Ticket<T>, where one function returns a Ticket<Func1Done> with the output and the other has to consume it as an input.
The typestate pattern is a specialization of making only valid states representable
throwaway17_17 2 hours ago
I know this wasn’t the crux of your post, but do you find that you primarily look at types as puzzles in a majority of your code? I have a fundamentally different view and find other perspectives interesting when thinking about language design.
As a separate point, I think this is an excellent example of making invalid states unrepresentable.
vatsachak 2 hours ago
Types are puzzles in a good way.
If you were to design Ikea furniture, you'd make pieces that only fit in to the total configuration the correct way.
Types provide that same phenomenon in programming imo. At the end of the day we are shoveling and playing with bytes so we need to provide handles to these processes which make sure that we can't fit a "square peg into a round hole"
Seattle3503 an hour ago
I think this is similar to what Axum does to make sure your router has its state declared before you try to instantiate it. Are there other examples in popular libraries?
LtdJorge 5 hours ago
One of my favorite useful patterns
throwaway894345 3 hours ago
I’ve been experimenting with Rust’s type state pattern—I’m trying to build something that builds an inventory of some object storage prefix (recording the version and size of each object in the prefix), but the pattern seemed so cumbersome. The goal was to avoid committing to a particular I/O color (sync vs async) and to have a testable no_std core, but I have so much less confidence in the typestate version compared to the “define traits for I/O and build an imperative loop around it”. I’m curious if anyone has suggestions (I realize it’s probably difficult to help without access to source code).
vatsachak 2 hours ago
Why is it cumbersome?
gardaani 38 minutes ago
throwaway894345 an hour ago
binary132 4 hours ago
why not just create a wrapper type for the payload that is returned by func1 and func2 takes it as a parameter?
vatsachak 3 hours ago
That's fair but then you have to make your args a struct for this bespoke purpose; an anti pattern.
Also, many other functions can depend on the ticket from func_1. So making the ticket separate and generic on the process is the right (imo) solution here.
throwawayqqq11 an hour ago
llleeeoooh 3 hours ago
Because you may want to share certain behaviors between the two wrapper types via generic impl
binary132 3 hours ago
doyougnu 5 hours ago
This was a talk at the FUNARCH workshop at this year’s ICFP.
Here’s the livestream: https://www.youtube.com/live/c0pw1iVs_Q0?is=hwm2xa4cZOcqF5tW
Well post the individual talks in the following days!
arpinum 5 hours ago
I use Typestates and Newtypes extensively. The metric that shows Typestate and Newtypes are beneficial is: How many method calls or parameters can be called / used that compile but are not valid use cases. You want to minimise this number. I love having a type state where I can only make 1 or 2 method calls because the state enforces there are only a few parsing / validation / transition methods available. And there is only one valid way to supply the parameters, I cannot use the strings in the wrong location. I only wish we had named parameters like ObjC.
throwaway17_17 2 hours ago
From a language design perspective I go back and forth on named params. I think the only conclusion I’ve reached is that I am not in favor of them being optional, but I think that is more a concern for implementation of the language and less about how it effects users.
How do you find the feature useful in this instance, I can’t quite picture how that works for typestate pattern functions.
bana-io 34 minutes ago
Maybe I am missing something but where is the entire source code?
michaelnoguera 8 hours ago
bana-io 37 minutes ago
You deserve a medal for that.
sourdecor 5 hours ago
Could someone compare this to ST in Idris?
throwaway17_17 2 hours ago
If you are asking in the context of Idris 2.0 (the current version), ST is not really related.
However, if you mean ST in Idris 1.0, there is a definite correlation. The mechanism that ST used for enabling local mutations was very similar to the mechanism that the typestate pattern in Rust is using. ST was a framework for formalizing State Machines in dependent types which is the mechanism TFA is analyzing.
vatsachak 3 hours ago
This is not really ST. This is analogous to eating at an old school restaurant.
You can't just walk in to the food service counter and say "give me a burger"; you need to first get a ticket from the cashier proving that you've ordered a burger and then provide that ticket to the guy at the counter.
That's literally the type state pattern
nvader 3 hours ago
Yes, I believe it should be possible for someone to do that.