3 ms·
Static typing is nice because it makes the IDE popup with the information. Shower that!
by matt3210 3y ago
Static typing is nice because it makes the IDE popup with the information. Shower that!
- ape4 3y agoYes 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
- the_gastropod 3y agoLanguage Servers do a pretty terrific job of this, too, basically for free.
- maerF0x0 3y agoDocs can do this too, obviously only as correct as the docs.
- wharvle 3y agoYou should be documenting most type info at least at the function/method signature and data structure definition level anyway. May as well do it in the most-useful format possible. This is what gets me about “even modern relatively-low-boilerplate type systems just slow me down” folks. That means you’re skipping documentation you ought to be doing.
- wharvle 3y agoYeah the main benefits are reducing bugs in developer write-run-debug cycles, and acting as machine-readable documentation (including to generate tooltips and autocomplete). The “cold shower” doesn’t check those.