Enums in Odin
A cargo enum, the .Name shorthand, and iteration over the type itself.
package main
import "core:fmt"
Cargo :: enum { General, Reefer, Hazmat }
main :: proc() {
load := Cargo.Reefer
// Enum values shorten to .Name where the type is known.
if load == .Reefer {
fmt.println("plug it in")
}
// The type itself is iterable.
for kind in Cargo {
fmt.println(kind, "=", int(kind))
}
}
How it works
Cargo :: enum { General, Reefer, Hazmat }declares the tags.- Where the type is known,
.Reeferneeds no prefix. for kind in Cargoiterates the enum's values;int(kind)shows the ordinals.
Keywords and builtins used here
CargoGeneralHazmatReeferenumforifimportinintpackageproc
The run, in numbers
- Lines
- 18
- Characters to type
- 310
- Tokens
- 66
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 57 seconds.
Step 2 of 3 in Structs, enums & unions, step 14 of 27 in Language basics.