News for hackers. Stuff that matters.
Re:Let the Type System do the Work by steveklabnik on Monday April 21, 2014 @01:22PM (#7622269)
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.ht...
Re:Let the Type System do the Work by sparkie on Monday April 21, 2014 @02:28PM (#7622713)
nitpick - 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.
Re:Let the Type System do the Work by jarrett on Monday April 21, 2014 @02:34PM (#7622751)
You'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.
Re:Let the Type System do the Work by dllthomas on Tuesday April 22, 2014 @06:30AM (#7626796)
And 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!).
Re:Let the Type System do the Work by steveklabnik on Monday April 21, 2014 @03:05PM (#7622979)
Whoops, thanks for the correction!
Re:Let the Type System do the Work by madisp on Monday April 21, 2014 @03:30PM (#7623129)
I 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.

Re:Let the Type System do the Work by judk on Monday April 21, 2014 @11:04PM (#7625531)
Float is an implementation detail. Precision is an API contract.
Re:Let the Type System do the Work by dllthomas on Tuesday April 22, 2014 @06:36AM (#7626811)
Right. 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.
Re:Let the Type System do the Work by fulafel on Monday April 21, 2014 @05:07PM (#7623883)
Floating point has dramatic differences in semantics compared to exact numbers, not an invisible implementation detail at all.
Re:Let the Type System do the Work by tormeh on Monday April 21, 2014 @05:24PM (#7624002)
Unless you're counting money or doing long-running simulations it's often more than good enough, especially Double.
Re:Let the Type System do the Work by dllthomas on Tuesday April 22, 2014 @06:40AM (#7626827)
Or doing discrete math, though you wouldn't be with a continuous quantity like Kilograms.
Re:Let the Type System do the Work by tutuca on Monday April 21, 2014 @02:27PM (#7622701)
or good ol' typedef, no?
Re:Let the Type System do the Work by masklinn on Monday April 21, 2014 @03:15PM (#7623044)
With 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.
Re:Let the Type System do the Work by dllthomas on Tuesday April 22, 2014 @06:25AM (#7626784)
"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.

Re:Let the Type System do the Work by steveklabnik on Monday April 21, 2014 @03:04PM (#7622968)
Sure, but typedefs aren't as strong a guarantee. They're more a textual find/replace than an actual feature of the type system.