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
沒有衝突。