F# Tutorial on F# Enumerations

an enumeration is a set of named integer constants.

in f#, enumerations, also known as enums, are integral types where labels are assigned to a subset of the values. you can use them in place of literals to make code more readable and maintainable.

declaring enumerations

the general syntax for declaring an enumeration is −

type enum-name =
   | value1 = integer-literal1
   | value2 = integer-literal2
...

the following example demonstrates the use of enumerations −

example

// 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

when you compile and execute the program, it yields the following output −

monday: mon
saturday: sat
sunday: sun