enum

Enums (enumerations) are a type of struct that contain a list of constants.

Number-valued enums start their members at 0 by default, but can be initialized to arbitrary values.1

My_Enum :: enum u16 {
    VALUE_ZERO,
    VALUE_ONE,
    VALUE_TWO,
    VALUE_THREE,
    VALUE_HIGH,
}

Introspection for enums includes count, value range, and names.2

printf("My_Enum ranges from %d to %d.\n", My_Enum.lowest_value, My_Enum.highest_value);
printf("My_Enum has %d members:\n", My_Enum.count);
for My_Enum.names {
    printf("  name: %s: value: %d\n", My_Enum.names[it_index], My_Enum.values[it_index]);
}

declaration

My_Enum :: enum u16 {
    VALUE_ZERO = 0,
    VALUE_ONE,
    VALUE_THREE = VALUE_TWO,
    VALUE_FOUR = MIDDLE_VALUE, // declared outside of enum
    VALUE_HIGH,                // trailing comma is fine
}

MIDDLE_VALUE := 8;

Enum values can be treated with strict or loose typing.3

x : My_Enum:strict;
x = My_Enum.VALUE_THREE; // valid
x = 10; // compiler error, even though 10 is a valid u16

y : My_Enum:loose;
y = 10; // valid
y = cast(My_Enum.VALUE_THREE, My_Enum.loose);

properties

  • count - number of members
  • highest_value - last value in the enum (may not be the largest numeric value of all the enum members)
  • lowest_value - first value in the enum (may not be the largest numeric value of all the enum members)
  • members - array of enum members
  • names - array of enum names
  • values - array of enum values

  1. enums are typed, can be named or anonymous, and can refer to values declared elsewhere.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=1058 

  2. an enum is actually syntactic sugar for a struct that has some elements that tells you things about the enum.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=2684 

  3. enums are strict types, distinct from all other types declared in the program, and can be treated with strict or loose typing behavior on a per-statement basis.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=2906 

jailang 2019 pixeldroid
https://github.com/pixeldroid/jailang
programming pages theme v0.5.21 (https://github.com/pixeldroid/programming-pages)