For example, types for SI units have been standardized in the F# core library, in Microsoft.FSharp.Data.UnitSystems.SI
. Open the appropriate sub-namespace, UnitNames
or UnitSymbols
, to use them. Or, if only a few SI units are required, they can be imported with type aliases:
/// Seconds, the SI unit of time. Type abbreviation for the Microsoft standardized type.
type [<Measure>] s = Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols.s
Some users tend to do the following, which should not be done whenever a definition is already available:
/// Seconds, the SI unit of time
type [<Measure>] s // DO NOT DO THIS! THIS IS AN EXAMPLE TO EXPLAIN A PROBLEM.
The difference becomes apparent when interfacing with other code that refers to the standard SI types. Code that refers to the standard units is compatible, while code that defines its own type is incompatible with any code not using its specific definition.
Therefore, always use the standard types for SI units. It doesn't matter whether you refer to UnitNames
or UnitSymbols
, since equivalent names within those two refer to the same type:
open Microsoft.FSharp.Data.UnitSystems.SI
/// This is valid, since both versions refer to the same authoritative type.
let validSubtraction = 1.<UnitSymbols.s> - 0.5<UnitNames.second>