More Units of Measure

In a previous post I looked at the unit of measure feature of F#. Now it’s time to revisit the topic.

So as a refresher, we can define a unit of measure in the following manner

[<Measure>]
type kg

and we use the unit of measure thus

let weight = 75<kg>

Now it’s important to note that the unit of measure feature only exists at compile time. At runtime there’s no concept of the unit of measure, i.e. we cannot use reflection or anything else to see what the unit used was as it no longer exists. However at compile time the code will not build if we try and use a value with a unit of measure on which is not compatible with another unit of measure.

But there’s more we can do with a unit of measure which makes them even more useful. We can supply a unit of measure with static member functions, for example

[<Measure>
type g = 
   static member toKilograms value : float<g> = value / 1000.0<g/kg>
and [<Measure> kg = 
   static member toGrams value : float<kg> = value * 1000.0<g/kg>

In the example above we use a mutually recursive type (using the and syntax). This allows the g type to use the kg type and vice versa.

With this code we can now write

let kilograms = g.toKilograms 123<g>

The result of this will assign the value kilograms with the result of the toKilograms function as well as setting it’s unit of measure to kg.

Whilst assigning a unit of measure to a numerical value is simple enough, to apply it to an existing value requires the use of an F# core function, from the LanguagePrimitives module.

This code creates a value with a unit of measure

[<Measure>] 
type g = 
    static member create(value : float) = LanguagePrimitives.FloatWithMeasure<g> value

So in the above code we might pass in a value and using the FloatWithMeasure function we apply the g unit of measure

let weight = 123
let weightInGrams = g.create weight

The weightInGrams value will now have the g unit of measure applied to it.