3 ms·
Depends, if you're very diligent, you could use a "smart constructor" (in some languages you can make it so there's only one way to construct a type, and then e
by theLiminator 3y ago
Depends, if you're very diligent, you could use a "smart constructor" (in some languages you can make it so there's only one way to construct a type, and then enforce validation at construction time) and instead of taking in a float/int for temperature, take in a Temperature or OutsideTemperature type as an argument.
An interesting implementation of this idea is outlined here too:
https://medium.com/iterators/to-tag-a-type-88dc344bb66c https://medium.com/iterators/to-tag-a-type-88dc344bb66c
A strictly newtype can be very powerful, but needing to "unwrap" your datatype to use it in normal operations of that datatype can be a little unwieldy so tagged types are a pretty interesting mechanism that provides type safety and convenience. I don't think it's strictly better though, it depends heavily on context.
- azemetre 3y agoWould something like this [1] be the function equivalent (not type equivalent) written in rust? A function that has a parameter of ints but will only yield numbers within a certain range? I guess I'm really confused by the Scala example you posted but really trying to understand. [1] https://doc.rust-lang.org/std/convert/trait.TryFrom.html https://doc.rust-lang.org/std/convert/trait.TryFrom.html