How to have strongly typed numerical values?

bearophile bearophileHUGS at lycos.com
Wed Sep 5 04:50:42 PDT 2012


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


More information about the Digitalmars-d-learn mailing list