Hi. Here's a short question which has been bothering me, and a longer
discussion of why. Apologies if it's a rote.
Where do units of measure fit into a type system?
Expressions along these lines should ideally be legal:
(x :: Metres) / (y :: Seconds)
(x :: Pounds_sterling_per_kilowatt_hour) * (y :: kilowatt_hours)
...and there's something to be said for supporting the addition of a
number of feet to a number of metres,
...but this should ideally be rejected by the compiler:
(x :: Metres) + (y :: Seconds)
It's tempting to use the type system, but then it runs into these two
problems (at least):
- There are infinitely many derivable units of measure. At what
point do you stop defining them singly and statically?
- There's a slew of (*) and (/) overloadings, none of which are fit
to be written by hand.
If a set of units is declared elsewhere than in your program, what
happens when another currency becomes of concern?
Could every numeric expression be accompanied by a set - inferred
where possible - of (unit, nonzero exponent) pairs? For example,
could a force expression have {(kg, 1), (m, 1), (s, -2)} as part of
its type?
Regards,
Tom