Hacker News new | ask | show | jobs
by thelinked 4318 days ago
Check out F#'s units of measure. http://msdn.microsoft.com/en-us/library/dd233243.aspx
2 comments

pjungwir's example code is actually really close to what you'd see in F#. In F# you'd typically write it in a more functional way with say a recursive inner function but I'll leave it imperative for clarity's sake:

    [<Measure>] type s
    [<Measure>] type m
    [<Measure>] type km  

    let mtokm (x:float<m>) = x * 1.<km>/1000.<m>
   
    //the types of g and ground are inferred
    let drop g ground (x0:float<m>) =
         let mutable x = x0  //type inferred here
         let mutable t = 0.0<s>
         let mutable v = 0.0<m/s> 
         let dt = 0.01<s>

         while x >= ground  do
            v <- v + g*dt
            x <- x + v*dt
            t <- t + dt 
         t  

   > drop -10.0<m/s^2> 0.<m> 50.<m>  
   val it : float<s> = 3.16
I've got to confess that I love how the design of MS languages regularly exposes my ignorance towards them.

Edit: removed unrelated Wikipedia article (Unit Type)

Unit type is completely different concept in the functional languages. It does not track units and measures.
Oh my, thanks for the heads-up I guess it's too late to post on HN. Time to go to bed :)