枚举是一组命名的整数常量.
在F#中,枚举,也称为枚举,是不可或缺的将标签分配给值的子集的类型.您可以使用它们代替文字来使代码更具可读性和可维护性.
声明枚举
声明枚举的一般语法是 :
type enum-name = | value1 = integer-literal1 | value2 = integer-literal2...
以下示例演示了枚举与减号的使用;
示例
// 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.Satlet weekend2 : Days = Days.Sunlet weekDay1 : Days = Days.Monprintfn "Monday: %A" weekDay1printfn "Saturday: %A" weekend1printfn "Sunday: %A" weekend2
当你编译并执行程序,它产生以下输出 :
Monday: MonSaturday: SatSunday: Sun