3 ms·
Check out F#'s units of measure. http://msdn.microsoft.com/en-us/library/dd233243.aspx http://msdn.microsoft.com/en-us/library/dd233243.aspx
by thelinked 12y ago
Check out F#'s units of measure. http://msdn.microsoft.com/en-us/library/dd233243.aspx http://msdn.microsoft.com/en-us/library/dd233243.aspx
- hoggle 12y agoI'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)
- Dn_Ab 12y agopjungwir'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