3 ms·
Yes and it will be checked in control paths that aren't always run. I guess a "cold shower" is that it only checks the type. You could put 999 into a variable
by ape4 3y ago
Yes and it will be checked in control paths that aren't always run.
I guess a "cold shower" is that it only checks the type. You could put 999 into a variable that's supposed to hold the outside temperature.
- david422 3y ago> You could put 999 into a variable that's supposed to hold the outside temperature I've never done this, but you could also define types for stuff like this. type: PositiveInteger, or type: BoundedTemperature that would only ever hold valid values. Or one thing where it might come in handy would be when dealing with user/potentially malicious input - SanitizedString etc.
- theLiminator 3y agoDepends, 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
- hoosieree 3y agoRIP static type advocates who forgot fires can happen outside.
- Nagyman 3y agoA reasonable type could be defined for outside temperature with predefined bounds, for example in ADA: type OutsideTempC is digits 3 range -100.0 .. 70.0; (Edit: others mentioned this)
- paxys 3y agoThere will obviously be a complexity tradeoff, but instead of using integer to pass this value around you can create an OutsideTemperature type that validates this rule.
- potsandpans 3y agoIf I'm understanding correctly, refinement types[1] solve this by allowing you to constrain a type into a smaller set of values. Think set notation: ``` type Temperature<min: number, max: number> = { x: number | x >= min && x <= max } ``` [1] - https://arxiv.org/pdf/2010.07763.pdf https://arxiv.org/pdf/2010.07763.pdf