枚举通常称为枚举。它是标签和值对的组合。标签被分配给值的子集。你可以使用它们代替文字, 以使代码更具可读性和可维护性。
句法:
type enum-name =
| value1 = integer-literal1
| value2 = integer-literal2
...
枚举与联合非常相似, 不同之处在于它允许我们指定值。
F#枚举示例【F#枚举】你可以使用标签名称或整数值来访问枚举。让我们来看一个例子。
type Year =
| January = 0
| Fabruary = 1
| March = 2
| April = 3// Use of an enumeration.
let monthName = enum<
Year>
(3)
printf "%A" monthName
let monthLiteral : Year = Year.January
let n = int monthLiteral
printf "\n%d" n
输出:
April
0