Why types cannot be selected for nested functions

I do not understand an output system like F # for nested functions. This seems to be especially difficult if I use types outside of simple types like int, string, ...

here is a small example of some code that prints some reflection information

let inferenceTest () =
    let t = int.GetType()
    let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
    printfn "%s" <| String.concat ", " methods

It's fine! No casting needed, etc. Now suppose that printing is much more active, so we want to split 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    

It is not possible to search for an object of an indefinite type based on information up to this point in the program. An annotation of the type may be required ... "

Why unexpectedly less information for the type system? Perhaps I could understand the problem if my function printType()were in the same area as mineinferenceTestFailsToCompile()

, t ,

let inferenceTestLambda () =
    let t = int.GetType()
    let printType =
        let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
        printfn "%s" <| String.concat ", " methods
    printType
+4
1

. , .

, .

let inferenceTest () =
    let t = int.GetType()
    let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
    printfn "%s" <| String.concat ", " methods

let inferenceTest () =
    let (t : type) = int.GetType()
    let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)
    printfn "%s" <| String.concat ", " methods

let inferenceTest () =
    let (t : type) = int.GetType()
    let methods = (t.GetMethods() : System.Reflection.MethodInfo []) |> Seq.map(fun m -> m.Name)
    printfn "%s" <| String.concat ", " methods

let inferenceTest () =
    let (t : type) = int.GetType()
    let (methods : seq<string>) = (t.GetMethods() : System.Reflection.MethodInfo []) |> Seq.map(fun m -> m.Name)
    printfn "%s" <| String.concat ", " methods

|> ,

let methods = Seq.map (fun m -> m.Name) (t.GetMethods())

.

let printType (t : 'a) () = 

let methods = t.GetMethods() |> Seq.map(fun m -> m.Name)

t t.GetMethods().

, Visual Studio , . , , , . .

EDIT:

F # ?

F # , , , , , .

.

", . , .

, , intellisense .

+5

Source: https://habr.com/ru/post/1628372/


All Articles