RequireQualifiedAccess
使用 RequireQualifiedAccess
属性,union case 必须被称为 MyUnion.MyCase
而不仅仅是 MyCase
。这可以防止封闭的命名空间或模块中的名称冲突:
type [<RequireQualifiedAccess>] Requirements =
None | Single | All
// Uses the DU with qualified access
let noRequirements = Requirements.None
// Here, None still refers to the standard F# option case
let getNothing () = None
// Compiler error unless All has been defined elsewhere
let invalid = All
例如,如果 System
已经打开,则 Single
指的是 System.Single
。与联合案件 Requirements.Single
没有冲突。