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
- Use variant names that clearly describe the value.
- Keep enum sets focused. Split unrelated states into separate enums.
- Review enum changes carefully when other code depends on their values.
Members and Values
Current enums are named groups of explicit constant values.
- Each member writes its value type and concrete value.
- Members are read with EnumName=>MemberName.
- Store enum results in the underlying value type, such as u32 or s32.
Closed Domains
Enums document the expected values for a small domain.
- Use enums for modes, status codes, tags, and external numeric constants.
- Keep values stable when they are part of a file format, ABI, or public API.
- When behavior depends on every enum member, review all matches or condition chains after adding a member.
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;
}