枚举
在F#中,enumerations(也称为enums)是整型类型,其中将标签分配给值的子集。 您可以使用它们代替文字,使代码更具可读性和可维护性。
声明enumerations
声明enumerations的一般语法是
type enum-name =
| value1 = integer-literal1
| value2 = integer-literal2
...
下面的例子演示了如何使用enumerations
例
// Declaration of an enumeration.
type Days =
| Sun = 0
| Mon = 1
| Tues = 2
| Wed = 3
| Thurs = 4
| Fri = 5
| Sat = 6
// Use of an enumeration.
let weekend1 : Days = Days.Sat
let weekend2 : Days = Days.Sun
let weekDay1 : Days = Days.Mon
printfn "Monday: %A" weekDay1
printfn "Saturday: %A" weekend1
printfn "Sunday: %A" weekend2
当你编译和执行程序,它产生以下输出
Monday: Mon
Saturday: Sat
Sunday: Sun