Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are units of measure represented?

I'm just starting with F# and am wondering about units of measure:

I understand they are only present at compile time and Reflector confirms this: creating a unit of measure that is not an alias creates a class with MeasureAttribute attribute, but other than that, float<km/h> is represented as plain System.Double. And there are no attributes on the type, constructor parameter or properties.

So far I understand. But when I reference the compiled assembly from another project, it seems to know all about the units of measure, including aliases. How does it get that information? Where in the assembly are they?

like image 321
svick Avatar asked Jun 23 '11 18:06

svick


People also ask

How do you represent units?

Units: The names of all units start with a lower case letter except, of course, at the beginning of the sentence. There is one exception: in "degree Celsius" (symbol °C) the unit "degree" is lower case but the modifier "Celsius" is capitalized. Thus, body temperature is written as 37 degrees Celsius.

What are the 7 unit of measurement?

The 7 Base Metric Units These dimensions are measurements of length, mass, time, electric current, temperature, amount of a substance, and luminous intensity. Here are definitions of the seven base units: Length: Meter (m) The meter is the metric unit of length.


1 Answers

F# stores 'extra type information' in a resource in the compiled assembly, and the F# compiler knows how to read that resource. So whereas a discriminated union is just compiled into, say, a class, and a unit of measure is erased into a double, there's extra F#-specific type info in a resource in the assembly so that when the F# compiler reads it, it can re-construct the extra "F# metadata".

The PowerPack has a metadata reader that lets you access it programmatically.

like image 76
Brian Avatar answered Sep 25 '22 07:09

Brian