3 ms·
This might be a good time to check out algebraic data types, esp. with a single constructor. For example, in Elm you might declare: type Length = Inches In
by ghayes 8y ago
This might be a good time to check out algebraic data types, esp. with a single constructor. For example, in Elm you might declare:
type Length = Inches Int
Then any function that accepts a Length, you can only pass in a length. This effectively acts like tagged tuples verified at compile time. You can later extend this type to be:
type Length = Inches Int | Meters Int
and any code that reads length would have to handle both cases (also checked at compile that).
I have found this to be a great way of ensuring all of your types are correct (and sometimes, a great frustration of wishing I didn't have to wrap/unwrap types so often).
- GavinMcG 8y agoYeah, that's one of my favorite takeaways from Elm. I'm thinking of it in an OO style, where perhaps instead of wrapping/unwrapping you're specifying an analog to object properties.
- insulanian 8y agoF# has units of measure [1], which are very nice for such a case you're describing. [1] https://fsharpforfunandprofit.com/posts/units-of-measure/ https://fsharpforfunandprofit.com/posts/units-of-measure/