Skip to content
IsaacShelton edited this page Nov 16, 2022 · 2 revisions

Enums

Enums are declared using the enum keyword, followed by a list of member names.

enum Color (RED, BLUE, GREEN, YELLOW, ORANGE, PURPLE, CYAN, PINK)

Enum Values

A specific member of an enum can be obtained using EnumName::MEMBER_NAME

Color::RED

See enum values for more information

Usage Example

import basics

enum Color (RED, GREEN, BLUE)

func main {
    favorite_color Color = Color::BLUE
    
    exhaustive switch favorite_color {
    case Color::RED
        print(“Favorite color is red”)
    case Color::GREEN
        print(“Favorite color is green”)
    case Color::BLUE
        print(“Favorite color is blue”)
    }
}

Foreign Enums

Enums from C libraries are commonly prefixed with their enum name. In these cases, it's nice to automatically define aliases so that the enum prefix is optional.

For example, the CURLUPart enum from libcurl has members named like CURLUPART_URL. Normally, we would have to use CURLUPart::CURLUPART_URL to specify that variant of the enum.

By making CURLUPart a foreign enum, we allow using CURLUPART_URL instead of CURLUPart::CURLUPART_URL.

foreign enum CURLUPart (
    CURLUPART_URL,
    CURLUPART_SCHEME,
    CURLUPART_USER,
    CURLUPART_PASSWORD,
    CURLUPART_OPTIONS,
    CURLUPART_HOST,
    CURLUPART_PORT,
    CURLUPART_PATH,
    CURLUPART_QUERY,
    CURLUPART_FRAGMENT,
    CURLUPART_ZONEID /* added in 7.65.0 */
)
Clone this wiki locally