Enums

enum types are similar to C enums.

#import "fmt.odin";

// Enums have a default backing type of `int` and implicitly start from 0
Number :: enum {
    ZERO, ONE, TWO,
}

// Enum with a specific backing type
Flag :: enum u8 {
    HOT  = 1<<0,
    COLD = 1<<1,
    YES  = 1<<2,
    NO   = 1<<3,
};

main :: proc() {
    // Enums will be printed as their name, not their value
    fmt.println("0 is", Number.ZERO);    

    // Enums can be cast as integers
    fmt.printf("%s is %d\n", Number.ZERO, cast(int)Number.ZERO);
    fmt.printf("%s is %d\n", Number.ONE,  cast(int)Number.ONE);

    f := Flag.HOT | Flag.YES;
    fmt.printf("flags = %b\n", f); // Print as a binary integer

}

The specific backing type for an enum can only be an integer or a float (at this current time).

Special values

An enum type contain special member values.

  • names a string slice of the values names
  • count number values
  • min_value minimum value of all the enum values
  • max_value maximum value of all the enum values

results matching ""

    No results matching ""