How to have strongly typed numerical values?

Paul D. Anderson paul.d.removethis.anderson at comcast.andthis.net
Wed Sep 5 12:32:28 PDT 2012


On Wednesday, 5 September 2012 at 11:50:12 UTC, bearophile wrote:
> Don Clugston:
>
>> I'd be interested to know if that idea is ever used in real 
>> code. I mean, it's a classic trendy template toy, but does 
>> anyone actually use it?
>
> As usual I don't have usage statistics.
>
> I like dynamic languages, like Python. But if you give me a 
> static type system, then I want something back from the 
> language. Detecting certain bugs at compile-time is one way to 
> pay me back.
>
> If you want most programmers to use a feature like units, it 
> needs:
> - Built-in, in the standard library, or easy to install;
> - To have a compact and very nice looking syntax;
> - Easy enough to use;
> - Flexible, to be able to represent all units you find in many 
> real problems;
> - Have zero or near-zero run-time costs, in both CPU and memory;
> - Give acceptable error messages.
>
> I think in F# people are using units often enough.
>
> If a units system will be present in Phobos and it will be very 
> good to use, maybe D programmers will use them.
>
> I am currently using Haskell a little, and I am enjoying giving 
> types to values to tell them apart in terms of their usage.
>
>
>> I say this because I've done a lot of physics calculation 
>> involving multiple complicated units, but never seen a use for 
>> this sort of thing.
>> In my experience, problems involving units are easily 
>> detectable (two test cases will catch them all).
>> The most insidious bugs are things like when you have used 
>> constants at 25'C instead of 20'C, or when you have a sign 
>> wrong.
>
> There are some famous problems caused by mixing units:
> http://lamar.colostate.edu/~hillger/unit-mixups.html
>
> Maybe this is the most famous:
> http://mars.jpl.nasa.gov/msp98/news/mco990930.html
>
> Bye,
> bearophile

FWIW, there was a proposal (JSR-275) to add support for units of 
measure to Java that was ultimately rejected. The implementation 
can be found at JScience.org, written and maintained by the 
website owner, Jean-Marie Dautelle, who also led the JSR team.

Paul



More information about the Digitalmars-d-learn mailing list