Enums

An enum is a set of integer values, where each value has an associated name. For example:

  1. enum Color
  2. Red
  3. Green
  4. Blue
  5. end

An enum is defined with the enum keyword, followed by its name. The enum’s body contains the values. Values start with the value 0 and are incremented by one. The default value can be overwritten:

  1. enum Color
  2. Red # 0
  3. Green # 1
  4. Blue = 5 # overwritten to 5
  5. Yellow # 6 (5 + 1)
  6. end

Each constant in the enum has the type of the enum:

  1. Color::Red #:: Color

To get the underlying value you invoke value on it:

  1. Color::Green.value #=> 1

The type of the value is Int32 by default but can be changed:

  1. enum Color : UInt8
  2. Red
  3. Green
  4. Blue
  5. end
  6. Color::Red.value #:: UInt8

Only integer types are allowed as the underlying type.

All enums inherit from Enum.

Flags enums

An enum can be marked with the @[Flags] attribute. This changes the default values:

  1. @[Flags]
  2. enum IOMode
  3. Read # 1
  4. Write # 2
  5. Async # 4
  6. end

The @[Flags] attribute makes the first constant’s value be 1, and successive constants are multiplied by 2.

Implicit constants, None and All, are automatically added to these enums, where None has the value 0 and All has the “or”ed value of all constants.

  1. IOMode::None.value #=> 0
  2. IOMode::All.value #=> 7

Additionally, some Enum methods check the @[Flags] attribute. For example:

  1. puts(Color::Red) # prints "Red"
  2. puts(IOMode::Write | IOMode::Async) # prints "Write, Async"

Enums from integers

An enum can be created from an integer:

  1. puts Color.new(1) #=> prints "Green"

Values that don’t correspond to an enum’s constants are allowed: the value will still be of type Color, but when printed you will get the underlying value:

  1. puts Color.new(10) #=> prints "10"

This method is mainly intended to convert integers from C to enums in Crystal.

Methods

Just like a class or a struct, you can define methods for enums:

  1. enum Color
  2. Red
  3. Green
  4. Blue
  5. def red?
  6. self == Color::Red
  7. end
  8. end
  9. Color::Red.red? #=> true
  10. Color::Blue.red? #=> false

Class variables are allowed, but instance variables are not.

Usage

Enums are a type-safe alternative to Symbol. For example, an API’s method can specify a type restriction using an enum type:

  1. def paint(color : Color)
  2. case color
  3. when Color::Red
  4. # ...
  5. else
  6. # Unusual, but still can happen
  7. raise "unknown color: #{color}"
  8. end
  9. end
  10. paint Color::Red

The above could also be implemented with a Symbol:

  1. def paint(color : Symbol)
  2. case color
  3. when :red
  4. # ...
  5. else
  6. raise "unknown color: #{color}"
  7. end
  8. end
  9. paint :red

However, if the programmer makes a typo, say :reed, the error will only be caught at runtime, while attempting to use Color::Reed will result in a compile-time error.

The recommended thing to do is to use enums whenever possible, only use symbols for the internal implementation of an API, and avoid symbols for public APIs. But you are free to do what you want.