4 ms·
You can even take this a step further: using `float` instead of `kilogram` is leaking an implementation detail. It's (probably) too low-level of information to
by steveklabnik 13y ago
You can even take this a step further: using `float` instead of `kilogram` is leaking an implementation detail. It's (probably) too low-level of information to actually be useful.
Luckily, more and more programming languages are making creating these simple types easier. Haskell:
newtype Kilogram = Integer
Rust:
struct Kilogram(int);
In fact, this blog post, while a bit outdated, shows an application of this idea, to solve string encoding issues for HTML templating: http://bluishcoder.co.nz/2013/08/15/phantom_types_in_rust.html http://bluishcoder.co.nz/2013/08/15/phantom_types_in_rust.ht...
- tutuca 13y agoor good ol' typedef, no?
- deleted 13y ago[deleted]
- steveklabnik 13y agoSure, but typedefs aren't as strong a guarantee. They're more a textual find/replace than an actual feature of the type system.
- masklinn 13y agoWith a straight typedef, you can add 3 feet to 5 meters and get 8. Probably not desirable. So you've got to typedef single-field structs instead.
- dllthomas 13y ago"So you've got to typedef single-field structs instead." Which, thankfully, don't add any runtime cost (as should be expected). As an aside, you don't actually need to typedef them at that point, but it saves you having to write "struct ..." everywhere so it's typically worthwhile.
- sparkie 13y agonitpick - the haskell version should be newtype Kilogram = Kilogram integer The issue with doing this of course, is that it's mostly useless to computation. You lose the ability to use mathematical operators because you're no longer an instance of Num, and even if you create the Num instance, or use -XGeneralizedNewtypeDeriving, you can't multiply a Kilogram by a Meter/Second for example, since the arguments to (*) must be of the same type. One would need to use a generic "Measure" type instead, where the unit is some metadata attached to it, and the Num instance implements the typechecking on units.
- jarrett 13y agoYou're quite right about the limitations imposed when you make your own dimensional types like that. It turns out the problem isn't trivial. Which is why I prefer to use a library like Dimensional. With Dimensional, you get plenty of units out-of-the-box, you can define your own if needed, and you can perform arithmetic in a fairly natural way.
- steveklabnik 13y agoWhoops, thanks for the correction!
- dllthomas 13y agoAnd unfortunately even with a "generic Measure type" you can't actually enforce anything at compile time. The "Num" typeclass was not very well designed (given the current facilities of the language, some of which didn't exist at the time Num was designed, so no aspersions cast at the designers!).
- madisp 13y agoI wouldn't ever say that using floats compared to, say, precision decimals in any language is leaking an implementation detail. As a user of a library/API I need to know if I can rely on it being precise or not.
- judk 13y agoFloat is an implementation detail. Precision is an API contract.
- dllthomas 13y agoRight. An interface exposed with integers may wind up using floats internally, or vice versa; numeric algorithms may wind up doing arbitrary things to precision and accuracy; &c. "You are passing around a float" puts some bounds on what can be delivered but that isn't really sufficient information when it matters and is useless fluff when it doesn't.
- fulafel 13y agoFloating point has dramatic differences in semantics compared to exact numbers, not an invisible implementation detail at all.