thp-lang.org/md/learn/data-structures/enums.md

492 B

Enums (Tagged unions)

Basic enums

enum Suit
{
    Hearts,
    Diamonds,
    Clubs,
    Spades,
}

val suit = Suit::Hearts

Enums with values

enum IpAddress
{
    V4(String),
    V6(String),
}

val addr_1 = IpAddress::V4("192.168.0.1")

match addr_1
case IpAddress::V4(ip)
{
    // code..
}
case IpAddress::V6(ip)
{
    // more code..
}

// Without the full qualifier
match addr_1
case ::V4(ip)
{
    // code...
}
case ::V6(ip)
{
    // even more code...
}