ZetCode

F# enum type

last modified December 2, 2026

In this article we work with enums, which give names to integer constants.

An enum (enumeration) is a type whose values are named constants of an integral type, usually int. Enums are .NET value types derived from System.Enum. They are used when the values must be numbers: for codes stored in files or databases, for bit flags, and for the many enums of the .NET libraries, such as DayOfWeek or ConsoleColor. Within F# code, a discriminated union is usually the better choice, since it can only hold its own cases and its cases may carry data.

Defining an enum

An enum is defined like a union whose cases have no data, but each case is given a value with =. The values make it an enum.

priority.fsx
type Priority =
    | Low = 0
    | Medium = 1
    | High = 2

let task = Priority.High

printfn "%A" task
printfn "%d" (int task)
printfn "%b" (task = Priority.High)

We define three priorities with the values 0, 1 and 2.

let task = Priority.High

A case is written with the name of the enum type in front of it.

printfn "%d" (int task)

The int function returns the number behind the name. %A prints the name of the case.

λ dotnet fsi priority.fsx
High
2
true

Cases need the type name

The cases of a union can be used on their own. The cases of an enum cannot, because an enum value is in fact a number, and the compiler does not make its names available without the type name.

unqualified.fsx
type Priority =
    | Low = 0
    | Medium = 1
    | High = 2

let task = High

We use the case High without Priority in front of it.

let task = High

The compiler does not find a value named High. With Priority.High the script compiles.

λ dotnet fsi unqualified.fsx
unqualified.fsx(6,12): error FS0039: The value or constructor 'High' is not defined.

Enums and integers

An enum converts to its underlying type with int, and an integer converts to an enum with the enum function. Any integer is accepted, even one that has no name in the enum.

conversions.fsx
type Priority =
    | Low = 0
    | Medium = 1
    | High = 2

let fromFile = [ 2; 0; 1; 7 ]

for code in fromFile do
    let p = enum<Priority> code
    printfn "%d -> %A (defined: %b)" code p (System.Enum.IsDefined p)

printfn "%b" (Priority.High > Priority.Low)
printfn "%A" (Priority.Medium + Priority.High)

We convert priority codes read from a file into enum values.

let p = enum<Priority> code

The type to convert to is given in angle brackets. The value 7 becomes a Priority as well, although no case has that value; it is then printed as a number. System.Enum.IsDefined checks whether a value belongs to a named case.

printfn "%b" (Priority.High > Priority.Low)
printfn "%A" (Priority.Medium + Priority.High)

Enums are compared by their values. Arithmetic works too and gives a value of the same enum type, here the unnamed value 3.

λ dotnet fsi conversions.fsx
2 -> High (defined: true)
0 -> Low (defined: true)
1 -> Medium (defined: true)
7 -> 7 (defined: false)
true
3

Matching on enums

Enum cases are matched as constant patterns. Since an enum can hold any value of its underlying type, a match on an enum always needs a wildcard rule.

matching.fsx
type Priority =
    | Low = 0
    | Medium = 1
    | High = 2

let deadline priority =
    match priority with
    | Priority.Low -> "next month"
    | Priority.Medium -> "next week"
    | Priority.High -> "today"
    | _ -> "unknown"

for code in [ 0; 2; 5 ] do
    printfn "%d: %s" code (deadline (enum<Priority> code))

The deadline function maps a priority to a deadline.

| _ -> "unknown"

Without the last rule the compiler reports warning FS0104: Enums may take values outside known cases. For example, the value 'enum<Priority> (3)' may indicate a case not covered by the pattern(s). A match on a union needs no such rule when all its cases are covered. Matching is covered in F# match expression.

λ dotnet fsi matching.fsx
0: next month
2: today
5: unknown

Other underlying types

The values of an enum can be of any integral type or char. The type of the first value decides the underlying type, and all cases must use the same type.

underlying.fsx
type Level =
    | Off = 0uy
    | Dim = 128uy
    | Full = 255uy

type Grade =
    | Excellent = 'A'
    | Good = 'B'
    | Poor = 'E'

printfn "%d bytes" sizeof<Level>
printfn "%A = %d" Level.Dim (byte Level.Dim)

let code: char = LanguagePrimitives.EnumToValue Grade.Good
let grade: Grade = LanguagePrimitives.EnumOfValue 'E'
printfn "%c %A" code grade

A lamp level is stored in a byte, a grade in a character.

| Off = 0uy

The uy suffix makes the value a byte, so a Level takes one byte in memory. Literal suffixes are listed in F# data types. The byte function converts a level back to its number.

let code: char = LanguagePrimitives.EnumToValue Grade.Good
let grade: Grade = LanguagePrimitives.EnumOfValue 'E'

There is no char conversion for enums, and the enum function works only for enums of int. The general functions are EnumToValue and EnumOfValue from the LanguagePrimitives module; the annotations tell them which types to convert between.

λ dotnet fsi underlying.fsx
1 bytes
Dim = 128
B Poor

Names and parsing

The System.Enum class has functions that list the cases of an enum and convert between names and values. This is useful when enum values are read from text.

names.fsx
open System

type Season =
    | Spring = 0
    | Summer = 1
    | Autumn = 2
    | Winter = 3

for s in Enum.GetValues<Season>() do
    printfn "%d %s" (int s) (string s)

printfn "%A" (Enum.GetNames<Season>())

let ok, season = Enum.TryParse<Season>("Summer")
printfn "%b %A" ok season

printfn "%A" (Enum.TryParse<Season>("winter", true))
printfn "%A" (Enum.TryParse<Season>("Monsoon"))

We list the seasons and parse season names.

for s in Enum.GetValues<Season>() do
    printfn "%d %s" (int s) (string s)

Enum.GetValues returns an array of all cases in the order of their values. The string function gives the name of a case.

let ok, season = Enum.TryParse<Season>("Summer")

Enum.TryParse returns a tuple: whether the text was a valid name, and the parsed value. With true as the second argument the case of the letters is ignored. When parsing fails, the value is the default of the enum, the case with value 0. Like enum, the function also accepts numbers such as "7", so a parsed value should be checked with Enum.IsDefined when that matters.

λ dotnet fsi names.fsx
0 Spring
1 Summer
2 Autumn
3 Winter
[|"Spring"; "Summer"; "Autumn"; "Winter"|]
true Summer
(true, Winter)
(false, Spring)

Flag enums

In a flag enum each case is a single bit, and a value may combine several cases. The [<Flags>] attribute marks such an enum, so that combined values are printed as a list of names.

flags.fsx
open System

[<Flags>]
type Access =
    | None = 0
    | Read = 1
    | Write = 2
    | Delete = 4

let editor = Access.Read ||| Access.Write
printfn "%A" editor

printfn "%b" (editor.HasFlag Access.Write)
printfn "%b" (editor &&& Access.Delete = Access.Delete)

let admin = editor ||| Access.Delete
let reader = admin &&& ~~~(Access.Write ||| Access.Delete)
printfn "%A" admin
printfn "%A" reader
printfn "%d" (int admin)

We combine access rights of a document.

let editor = Access.Read ||| Access.Write

The values 1, 2 and 4 are powers of two, so each takes a different bit. The bitwise OR operator ||| sets both bits. Bitwise operators are covered in F# operators. The None case names the value with no bits set; since enum cases are always written with the type name, it does not clash with the None of options.

printfn "%b" (editor.HasFlag Access.Write)
printfn "%b" (editor &&& Access.Delete = Access.Delete)

HasFlag checks whether a flag is set; the bitwise AND &&& does the same by keeping only the bits of the flag.

let reader = admin &&& ~~~(Access.Write ||| Access.Delete)

Flags are removed with AND and the complement ~~~ of the flags to remove.

λ dotnet fsi flags.fsx
Read, Write
true
false
Read, Write, Delete
Read
7

.NET enums

Many .NET types use enums. A DateTime value reports its day of the week as a DayOfWeek, an enum whose values start with Sunday as 0.

weekdays.fsx
open System

let dates = [ DateTime(2026, 12, 2); DateTime(2026, 12, 5); DateTime(2026, 12, 6) ]

for date in dates do
    let day = date.DayOfWeek
    let kind =
        match day with
        | DayOfWeek.Saturday | DayOfWeek.Sunday -> "weekend"
        | _ -> "workday"
    printfn "%s %A (%d) %s" (date.ToString "yyyy-MM-dd") day (int day) kind

printfn "%A" ConsoleColor.DarkGreen

We find out which of three dates fall on a weekend.

| DayOfWeek.Saturday | DayOfWeek.Sunday -> "weekend"
| _ -> "workday"

Two enum cases are combined in an or-pattern. The wildcard covers the other five days and any value outside the cases.

λ dotnet fsi weekdays.fsx
2026-12-02 Wednesday (3) workday
2026-12-05 Saturday (6) weekend
2026-12-06 Sunday (0) weekend
DarkGreen

File permissions

The last example models Unix file permissions. Each file has read, write and execute rights for its owner, its group and all other users. The rights of each class fit in three bits and are written as one octal digit, so the whole mode is three digits such as 755.

permissions.fsx
[<System.Flags>]
type Perm =
    | None = 0
    | Execute = 1
    | Write = 2
    | Read = 4

let symbolic (p: Perm) =
    let bit flag letter = if p.HasFlag flag then letter else "-"
    bit Perm.Read "r" + bit Perm.Write "w" + bit Perm.Execute "x"

let fromDigit (d: char) = enum<Perm> (int d - int '0')

let show (name: string) (mode: string) =
    let owner = fromDigit mode[0]
    let group = fromDigit mode[1]
    let others = fromDigit mode[2]
    let text = symbolic owner + symbolic group + symbolic others
    let groupCanWrite = group.HasFlag Perm.Write
    printfn "%-12s %s %s  group can write: %b" name mode text groupCanWrite

show "backup.sh" "755"
show "notes.txt" "640"
show "id_ed25519" "600"
show "shared/" "775"

A mode such as 640 is turned into three flag values and printed in the symbolic form used by ls -l.

let fromDigit (d: char) = enum<Perm> (int d - int '0')

The digit is converted to its numeric value, which is already the combination of flags: 6 is 4 + 2, read and write.

let bit flag letter = if p.HasFlag flag then letter else "-"

The local function prints the letter of a right when its flag is set, or a dash when it is not.

λ dotnet fsi permissions.fsx
backup.sh    755 rwxr-xr-x  group can write: false
notes.txt    640 rw-r-----  group can write: false
id_ed25519   600 rw-------  group can write: false
shared/      775 rwxrwxr-x  group can write: true

Source

F# language reference: Enumerations

.NET API: System.Enum

In this article we have worked with enums: defining them, converting between enums and integers, matching on enums, other underlying types, names and parsing, flag enums, and .NET enums. Enums are value types; the other value types are covered in F# struct types. When an enum is passed where an obj is expected, it is boxed, see F# obj type and F# boxing and unboxing.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all F# tutorials.