This question is closely related to these ones (1, 2, 3)
I'm using an external library which doens't (yet) handle units of measure. I want to be able to 'ununit' values before I pass them in, then 'reunit' them when I get the results back.
The catch is that I'd like to avoid being forced to declare WHICH units in advance.
Example snippet
let ExternalNonUnitAwareFunction s = s + 1.
let MyUnitAwareClient (s:float<'u>) = //'
//1. this option "flattens" to no unit, or fixes to first inferred unit
//let (unit:float<'u>) = 1.0<_>
//2. this works fine, except for 0!
let unit = s / (float s)
s |> float |> ExternalNonUnitAwareFunction |> (*) unit
I haven't managed to work out how to handle this one...
Update If I have understood correctly, the final version of F# will include functions to do this.
And just for fun, here is the opposite:
Test :
Weird stuff with that #"(£$! Value restriction error in there if you run
let withunits = reunit nounits
on it's own. So you have to run it with the line that uses withunits. I guess it's not surprising, you have to pass in the(v:float<'u>)
to reunit for F# to be able to work out what 'u is. Possibly makes reunit of limited interest, I guess...UPDATE: A slightly kooky workaround is to pass in 'model' values
For now, boxing and casting appears to work:
I wouldn't be surprised if the units of measure stuff goes through some further changes before release, though, which might break this. You can also make a more generic version, such as:
EDIT
There is now a
FloatWithMeasure
function to 'cast to units':http://msdn.microsoft.com/en-us/library/ee806527(VS.100).aspx