[racket] Numbers with dimensions

From: Thomas Chust (chust at web.de)
Date: Fri Oct 25 10:41:58 EDT 2013

On 2013-10-25 14:29, Konrad Hinsen wrote:
> [...]
> A quick Google search led me to 
> 
>   http://en.wikibooks.org/wiki/F_Sharp_Programming/Units_of_Measure
> 
> which says
> 
>    "Important: Units of measure look like a data type, but they
>     aren't. .NET's type system does not support the behaviors that
>     units of measure have, such as being able to square, divide, or
>     raise datatypes to powers. This functionality is provided by the F#
>     static type checker at compile time, but units are erased from
>     compiled code. Consequently, it is not possible to determine
>     value's unit at runtime."
> 
> So units were added as a special case to the type checker, rather than
> implemented in terms of a generic type system. That approach should be
> adaptable to any language that has some type checker to start with.
> [...]

Hello,

that description sounds somewhat misleading to me. The implementation of
units of measure in F# is similar to the implementation of generic type
parameters in JVM languages: From the point of view of the type system,
units of measure are first class citizens in F#, they are real types as
far as the compiler is concerned, they are just not stored with other
runtime type information and thus cannot be queried at runtime.

For example you can write a function in F# that performs computations
with units and the conformity of the units will be checked, the units of
the return type and some arguments may be inferred. Only when you
convert from a type with a unit into something more generic, you can't
go back safely: You can cast from float<meter/second> to obj, but you
cannot determine by pattern matching / RTTI whether an obj is actually a
float<meter/second> or a float<kilogram>, since the runtime type
identification will just reveal whether that value is some kind of float.

You are also right, though, that the support for units in F# is a
special case in the sense that it cannot easily be leveraged for other
user-defined extensions of the type system.

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


Posted on the users mailing list.