typestar

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

  1. Cargo :: enum { General, Reefer, Hazmat } declares the tags.
  2. Where the type is known, .Reefer needs no prefix.
  3. for kind in Cargo iterates the enum's values; int(kind) shows the ordinals.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Structs, enums & unions, step 14 of 27 in Language basics.

← Previous Next →

Enums in other languages