I was just reading the following page: Units of measure | F# for fun and profit and the last paragraph states:
Units of measure at runtime
An issue that you may run into is that units of measure are not part of the .NET type system.
F# does stores extra metadata about them in the assembly, but this metadata is only understood by F#.
This means that there is no (easy) way at runtime to determine what unit of measure a value has, nor any way to dynamically assign a unit of measure at runtime.
It also means that there is no way to expose units of measure as part of a public API to another .NET language (except other F# assemblies).
So, I thought that maybe the information about units of measure should not be entirely based on generics, instead utilizing annotations, or maybe it is possible to combine generics and annotations in some sensible way? Could annotation processors help making this work during compile time? I have never used them myself.
@Unit(meter / second^2)
val acceleration = 2.0
@Unit(second)
val time = 10.0
val speed = acceleration * time
speed.unit // meter / second
@Unit(pascal)
val pressure = 10.0
speed + pressure // Runtime unit error, ideally we'd like to check this at compile time (annotation processors?)