How does F # infer types and tags from other modules?

Here is an example of the minimal code that I use to explain my problem. The following code is organized in two files and compiles in order:

DataStruct.fs

module MyMod 
type XXX = {
    a: int
}
with
    static member GetNew =
        {
            a = -1
        }

type YYY = {
    a: float
}
with
    static member GetNew =
        {
            a = -1.0
        }


type Choice =
    | XXX of XXX
    | YYY of YYY

Program.fs

open MyMod

let generator = 
    let res = XXX.GetNew
    Choice.XXX res

let myVal : XXX = 
    match generator with
    | XXX x -> x
    | _ -> printfn "expected XXX, got sth else!"; XXX.GetNew

Interestingly, I have a select type that has two tags that are named the same as the ones they label. This, as I understand it, is a general convention in F #.

Now I am changing the DataStruct so that I put it in the namespace and make MyMod just one of the modules in this namespace. Accordingly, in Program.fs I open the namespace and use all the prefix with the module name:

DataStruct.fs

namespace DataStruct

module MyMod =
    type XXX = {
        a: int
    }
    with
        static member GetNew =
            {
                a = -1
            }

    type YYY = {
        a: float
    }
    with
        static member GetNew =
            {
                a = -1.0
            }


    type Choice =
        | XXX of XXX
        | YYY of YYY

Program.fs

open DataStruct

let generator = 
    let res = MyMod.XXX.GetNew
    MyMod.Choice.XXX res

let myVal : MyMod.XXX = 
    match generator with
    | MyMod.XXX x -> x
    | _ -> printfn "expected XXX, got sth else!"; MyMod.XXX.GetNew

Program.fs . , GetNew, : ", " GetNew " " , MyMod.XXX MyMod.Choice.

, , Choice, , , .

DataStruct.fs, ,

type Choice =
    | TX of XXX
    | TY of YYY

Program.fs

open DataStruct

let generator = 
    let res = MyMod.XXX.GetNew
    MyMod.Choice.TX res

let myVal : MyMod.XXX = 
    match generator with
    | MyMod.TX x -> x
    | _ -> printfn "expected XXX, got sth else!"; MyMod.XXX.GetNew

GetNew , MyMod.XXX , .

, , F #? , , . , , , ?

+4
2

โ€‹โ€‹. โ€‹โ€‹, , . . , : . MyMod.XXX , DU. , codepath, , .

visualfsharp fsharp

+1

, Program.fs:

  • , open MyMod,

  • , open DataStruct, , . open DataStruct.MyMod, , .

:

  • , F # XXX .
  • , XXX XXX, MyMod. XXX - , - , Choice, XXX. , , , ILSpy.

: . F # XXX DU, . . .

+5

All Articles