Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Andrew Kennedy: F# Units of Measure

32 minutes, 44 seconds


Right click “Save as…”

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.


Information sources: http://msdn.com and http://blogs.msdn.com/andrewkennedy/


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
  • Bent Rasmussenexoteric stuck in a loop, for a while

    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.

  • AuxonRichard.Hein Read it: ​http://bitc​oin.​org/bitcoin.​pdf

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

  • keith flokeithfl 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!!!



  • NovoxNovox “Simplicity is the ultimate ​sophisticat​ion.” —Leonardo da Vinci

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

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

  • Allan LindqvistaL_ Kinect 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



    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.

  • CharlesCharles Welcome Change

    My apologies for the poor resolution of the code demo on-screen. The way that we captured this is the problem. We are working on a solution to this general problem. See, we don't expect interviewees to have to install screen capture software on their laptops in order to record a demo in the studio. That said, we aren't yet sure of a practical solution that also affords least effort on the part of our guests. Working it out.


    Thank you in advance, Andrew, for sharing the code in project file format. The domo code should be much easier to read in Visual Studio or a text editor Smiley


    Again, we will work this out.



  • Allan LindqvistaL_ Kinect 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?

  • keith flokeithfl keithfl

    Thanks for your reply ... Angel again ... a very interesting feature and a awesome interview!


  • Measurements in F# is an exciting feature. No doubt about it. I was really impressed when I learned about it being added to the language.

    It surely addresses a lot of potential issues with measurement mismatch. 


    Yet, run time support for measurements still makes a lot of sense.

    If you are reading data from external sources at run-time (files, sensors, or web services), you'd still have to implemet all the measurement tracking and conversion yourself. If this can be married to contract somehow, then application would just have to tell the framework that it expects mass to be in kilos, and if input feed turns out to be pounds then conversion would happen under the scene.


    Another presumably useful feature would be to declare measures off of classes/types. If for example, I'm counting my chicken, I don't want to be able to inadvertently add this to the count of eggs, unless I explicitly coersed chicken and eggs to be "things".


    Anyhow, compile time support is a very good start.

  • This is a feature that I have been waiting for.


    How is the UOM information able to be persited?  If I were serialize an object with UOM would the UOM be included in the XML?


    Also I read last year that this feature would also automatically take care of some of the unit conversion, say within a calculation.  Is this still possible or will we have to manage the unit conversion ourselves?


    Awsome stuff!

  • It's fairly stable. Defining your own overloaded methods on unit-parameterized types is slightly easier, as type inference has been improved. For the product release, there will be a bunch of functions to "cast" into units (e.g. one with type float -> float<'u>) which are intended for interop with non-unit-aware code. (e.g. you might have a numeric library that you want to wrap with a units API.)

  • The units-of-measure information is not persisted, as it is "erased" from types before runtime. (It's a purely static feature.)


    As for unit conversions, there are no plans for automatic conversions for this release of F#: you must define your own conversion functions.

  • >> See, we don't expect interviewees to have to install screen capture software on their laptops in order to record a demo in the studio. >> That said, we aren't yet sure of a practical solution that also affords least effort on the part of our guests

    Just Remote Desktop into the guest's laptop while recording the screencast on the client-side.


    Rant: why is Channel9 reliably not working with Firefox browsers?! Impossible to view videos or post replies. Bummer!

  • CharlesCharles Welcome Change

    Not sure about the FF issues. Can you be more specific? You could also run FF's dev tools and send the script error details Smiley


    Sorry you having a bad experience. We will look into this.


  • CharlesCharles Welcome Change

    Comments open.


  • David Phillip OsterDavid Phillip Oster

    C++ has had this a feature for decades, implemented as a template-library evaluated at compile time. See:  "The SI Library of Unit-Based Computation."
    Fermi National Accelerator Laboratory FERMILAB-Conf-98/328 
    "Introduction to the SI Library of Unit-Based Computation" by 
    Walter E. Brown 
    October 1998
    Presented at the International Conference on Computing in High Energy Physics (CHEP ’98), Chicago, Illinois, August 31-September 4, 1998
    When I got my copy, it was linked from  www.fnal.gov/fermitools

  • VivekVivek

    What David said!

Remove this comment

Remove this thread


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.