Enums (v0.2.1)

Named constants grouped under a common enum name.

Latest

Overview

Enums group related named values under one name. In current Thrust, each member writes its type and value explicitly.

Members are accessed with the enum access operator, for example Mode=>Safe. The expression evaluates to the member value.

An enum name is not used as a variable type in the current compiler. Store the underlying value type, such as u32 or s32.

Primary source: syntax/enum/

Syntax Signatures

enum Mode @public {
    Fast: u32 = 0;
    Safe: u32 = 1;
    Debug: u32 = 2;
}

Behavior and Use

Treat the listed members as the expected values for that domain.

If those values must match an external format, keep that mapping in one place.

Best Practices

Members and Values

Current enums are named groups of explicit constant values.

Closed Domains

Enums document the expected values for a small domain.

Example

enum ExitCode @public {
    Ok: s32 = 0;
    InvalidInput: s32 = 1;
    IOError: s32 = 2;
}

fn codeOf(value: s32) s32 @public {
    if value == ExitCode=>Ok {
        return 0;
    }

    if value == ExitCode=>InvalidInput {
        return 1;
    }

    return ExitCode=>IOError;
}

Back to language reference