Enums and flags
Enums
An enum value has a fixed set of possibilities.
Short enums can be squeezed onto a single line.
Declaring an enum implicitly declares many functions:
| Function | Description |
|---|---|
== bool(a direction, b direction) | Exact equality. |
<=> comparison(a direction, b direction) | Enum members declared earlier are less. |
hash void(a direction, state hash-state) | Hashes the numeric value. |
to json(a direction) | json value that is the name of the enum member. |
to symbol(a direction) | The name of the enum member. |
to direction?(a symbol) | Gets an enum member by name. |
to nat(a direction) | Numeric value of the enum member. |
to direction?(a nat) | Gets an enum member by value. |
members direction[]() | Array of every enum member. |
Conversion from symbol or nat returns an option,
because not all symbols and nat are valid directions.
"match" on enums
A more convenient way to use an enum is with a match expression.
Enum numbers
You can customize the number values used for the enum.
Flags
A flags value can contain any combination of the declared flags.
Declaring a flags type implicitly defines all of the same functions as for an enum,
plus several additional functions.
In the above example those are:
| Function | Use |
|---|---|
new actions() | Value with no flags set. |
carry actions() | Value with just the carry flag set. |
eat actions() | Value with just the eat flag set. |
toss actions() | Value with just the toss flag set. |
~ actions(a actions) | Value with the opposite flags set. |
| actions(a actions, b actions) | Union; contains any flags that appear in either argument. |
& actions(a actions, b actions) | Intersection; contains only the flags that both arguments agree on. |
subscript bool(a actions, b actions) | True if a contains every flag in b.
Written as a[b]. |
flags types also support all the usual auto functions.