F#命名约定是否禁止在不同光盘中具有相同的名称.工会类型?

Dim*_*mka 2 f#

我在一个模块中有一个共同类型,该类型在另外两个有区别的联合类型中使用.

为方便起见,我将它们命名为同名.其他名称不同.接下来,我正在尝试制作一个在控制台中打印类型的帮助器.

第一行无法编译,因为匹配大小写中的类型不匹配.我尝试了一些东西,无论是否打开模块,强制类型等,它仍然会失败.

另一件事是,如果我将鬃毛更改为Common1和Common2,它的工作没有任何问题.

我记得读过具有相同签名的类型存储在同一内部类型中,但我的现实示例有不同的签名但仍然失败.

我在某个地方错过了一点吗?

例:

示例无法编译错误:

错误此表达式应具有类型OneA,但此处具有类型OneB

在此输入图像描述

module commonThings =
    type CommonThing = 
        | Comm1 of int 
        | Comm2 of int 

module thingA =
    open commonThings
    type OneA = 
        | A1 of string
        | Common of CommonThing        
        | A2 of string

module thingB =
    open commonThings
    type OneB = 
        | B1 of string
        | Common of CommonThing        
        | B2 of string

module printAB =
    open commonThings
    open thingA
    open thingB

    let printA (msg:OneA) = 
        match msg with
        | A1 v -> printfn "A1"
        | Common v -> printfn "Common"
        | A2 v -> printfn "A2"

module main =

    [<EntryPoint>]
    let main argv = 
        printfn "%A" argv
        0 // return an integer exit code
Run Code Online (Sandbox Code Playgroud)

Lee*_*Lee 5

您可以通过为类型名称添加前缀来消除歧义:

let printA (msg:OneA) = 
    match msg with
    | A1 v -> printfn "A1"
    | OneA.Common v -> printfn "Common"
    | A2 v -> printfn "A2"
Run Code Online (Sandbox Code Playgroud)


scr*_*wtp 5

当您打开thingB模块时,键入OneB到范围中,类型中的Common案例标签将与类型中的案例标签相OneA混淆OneB.

当类型或联合/活动模式案例之间发生名称冲突时,最近的一个胜出.重新排序打开将使它偶然工作:

open thingB
open thingA
Run Code Online (Sandbox Code Playgroud)

正确的解决方案是为案例名称添加前缀.还有一个RequireQualifiedAccess属性可用于强制类型(或模块)始终需要其内部的前缀.