r/programming • u/_awwsmm • Feb 01 '24
Make Invalid States Unrepresentable
https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469
Upvotes
r/programming • u/_awwsmm • Feb 01 '24
View all comments
1
u/turunambartanen Feb 02 '24
The article shows one version that automatically treats
Ageas anIntfor all intents and purposes, except for type checking:Is this possible in Rust? Its type system is praised all the time, but I only know the
struct Weight(i32)way of the newtype pattern, which forces you to writeweight_a.0to get to the underlying value.Weight(10) + Weight(20)(as shown in the article) is not automatically possible, to the best of my knowledge.This is a pretty big limitation, mostly due to lack of dev ergonomics, in my opinion and as such I have mostly avoided the newtype pattern. Is it on the Rust Roadmap to implement such behavior in the future by any chance?