Posted By: Charles | Sep 22nd @ 2:00 PM | 31,780 Views | 16 Comments
Floating point values in F# can have associated units of measure, which are typically used to indicate length, volume, mass, and so on. The built-in type float takes an optional unit-of-measure parameter, written in angle brackets, in a similar way that types such as IEnumerable take a type parameter, as in IEnumerable<int>.

By using quantities with units, you enable the compiler to verify that arithmetic relationships have the correct units, which helps prevent programming errors like the one that led to NASA's Mars Climate Orbiter being lost in September 1999. This was due to confusion between metric and so-called "English" units of measurement.  The accident could have been prevented if the NASA engineers had been able to annotate their program code with units, and then employed static analysis tools or language-level type-checking to detect and fix any unit errors.

Andrew Kennedy is an MSR research scientist who implemented units of measure for F#. What did this involve? How does it work, exactly? What's next? Meet Andrew and learn all about F#'s latest language feature, units of measure.

Enjoy.

Information sources: http://msdn.com and http://blogs.msdn.com/andrewkennedy/
Rating:
3
0
exoteric
exoteric
I : Next<I>

I was very impressed when seeing units of measure in F# the first time and believe it's a great selling point for the language.

Richard.Hein
Richard.Hein
... my guitar gently weeps ...

Thanks, I find this very interesting.  I imagine it can be helpful in many situations. 

keithfl
keithfl
keithfl

Very interesting ...and deep ... another nice interview Charles!

 

We've seen Don Syme several times on C9 ... its nice to finally see Andrew Kennedy!

 

However ...  the code is completely unreadable!!! Perplexed C'mon cant you guys ever use a larger font? Try 14 please!! Or Just use the new Zoom feature in the VS2010 editor!!

 

I'd like to actually see the code!!!

 

 

One of my favorite F# features. Thanks for this great interview!

(and unfortunately keithfl is right about the unreadable code Expressionless )

aL_
aL_
Rx ftw

interesting Smiley so this is strictly a compile time feature? its not "generics" really, its sort of compile time generics?

thats the impression i got Smiley

  

--edit--

also, is this just for floats? can a unit be any type? for instance if you have a class Person, could you create a unit of measure for Employee, manager and so on? (just as an example)

Yes, this is strictly a compile-time feature, so units errors get caught at development time, and there is no runtime performance hit.

 

It's for any types you like. Built-in types float, float32, decimal, and int can be parameterized on units, but you can build your own, e.g. vectors, complex numbers, matrices, or whatever. My blog (http://blogs.msdn.com/andrewkennedy) has more info.

Ah - sorry about the unreadable code. I had been assuming that the screen would be captured in high-res. A lot of the examples that I used are also discussed on my blog. The Lunar Lander code was written by Phil Trelford but I'll put it back together and post it here shortly.

aL_
aL_
Rx ftw

very cool.. we do alot of physics calculations for measuremnts on lasers so this could be very useful Smiley are you making any large changes in beta2 to this or is the beta1 functionality kinda stable?

Microsoft Communities