I don't understand the F# type inference system for nested functions. It seems particularly broken when I use types outside simple types such as int, string, ...
here is a small example of some code printing some reflection info
let inferenceTest () =
let t = int.GetType()
let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
printfn "%s" <| String.concat ", " methods
This works fine! No need for casting etc. Now assume that the printing is much more involved, hence we want to separate it into a nested function
let inferenceTestFailsToCompile () =
let printType t =
let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
printfn "%s" <| String.concat ", " methods
let t = int.GetType()
printType t
This fails with "lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed..."
Why is there suddenly less information for the type system? Perhaps I could understand the problem had my printType()
function been in the same scope as my inferenceTestFailsToCompile()
When I instead create a lambda that takes t
as its closure, the typing issue goes away
let inferenceTestLambda () =
let t = int.GetType()
let printType =
let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
printfn "%s" <| String.concat ", " methods
printType