2 ms·
With an expressive enough type system you could also leverage the Typestate Pattern. This would have the further advantage of not only taking care of the final
by weinzierl 3y ago
With an expressive enough type system you could also leverage the Typestate Pattern. This would have the further advantage of not only taking care of the final step but also the intermediate ones.
For example in the first example from the article calling xml.attribute doesn't make sense without a prior xml.begin_element.
By using the Typestate Pattern the compiler would ensure that you can only call xml.attribute after a xml.begin_element. Of course all of this only works in cases where the order of steps is fixed at compile time.
EDIT: Please read chrismorgan's comment why, at least in Rust, this does unfortunately not really solve the "Obvious Final Step" problem.
https://news.ycombinator.com/item?id=36095908 https://news.ycombinator.com/item?id=36095908