This came up while I looked at the following question: F# Unit of Measure, Casting without losing the measure type Please note that I am not trying to use this unbox code, I just discovered some weird behavior while answering the question.
Why does the following code work
let intToFloat (x:int<'u>) : float<'u> = unbox float x
intToFloat 1<second>
while this yields a System.InvalidCastException: Unable to cast object of type 'float32ToFloat@86-6' to type 'Microsoft.FSharp.Core.FSharpFunc`2[System.Single,System.Double]'.?
let float32ToFloat (x:float32<'u>) : float<'u> = unbox float x
float32ToFloat 1.0f<second>
If I put parantheses around the (float x)
the code works as expected, so I assume it must be some expression evaluation/type inference rule. What exactly is going on here and why are the parantheses needed in the second case?