Tutorial by Examples

Units of measure are additional type annotations that can be added to floats or integers. They can be used to verify at compile time that calculations are using units consistently. To define annotations: [<Measure>] type m // meters [<Measure>] type s // seconds [<Measure>] typ...
[<Measure>] type m // meters [<Measure>] type cm // centimeters // Conversion factor let cmInM = 100<cm/m> let distanceInM = 1<m> let distanceInCM = distanceInM * cmInM // 100<cm> // Conversion function let cmToM (x : int<cm>) = x / 100<cm/m> l...
When a function doesn't preserve units automatically due to lower-level operations, the LanguagePrimitives module can be used to set units on the primitives that support them: /// This cast preserves units, while changing the underlying type let inline castDoubleToSingle (x : float<'u>) : fl...
The [<Measure>] attribute can be used on type parameters to declare types that are generic with respect to units of measure: type CylinderSize<[<Measure>] 'u> = { Radius : float<'u> Height : float<'u> } Test usage: open Microsoft.FSharp.Data.UnitSystems...
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 uni...

Page 1 of 1