Copyright © 2024 Sebastiaan Heins
enum
is a master keyword, it is used to create a enum definition.
An enum is a collection of named constants.
Example of a simple enum definition:
enum Color {
RED,
GREEN,
BLUE
}
do say (Color->RED) // 0
do say (Color->GREEN) // 1
do say (Color->BLUE) // 2
The syntax for creating a enum is as follows:
1. After the enum keyword, you write the name of the enum.
2. A enum body, this is enclosed in curly braces.
3. Each constant is separated by a comma.
4. Each constant is assigned a value, starting at 0.
You can also set specific values for each constant.
Example of a enum definition with specific values:
enum Color {
RED = 1,
GREEN = 2,
BLUE = 4
}
do say (Color->RED) // 1
do say (Color->GREEN) // 2
do say (Color->BLUE) // 4
You can mix match, it'll start counting again from the last value.
Example of a enum definition with mixed values:
enum Color {
RED = 5,
GREEN,
BLUE = 10
}
do say (Color->RED) // 5
do say (Color->GREEN) // 6, because RED is 5
do say (Color->BLUE) // 10