abstract struct Enum

Overview

Enum is the base type of all enums.

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

enum Color
  Red   # 0
  Green # 1
  Blue  # 2
end

Values start with the value 0 and are incremented by one, but can be overwritten.

To get the underlying value you invoke value on it:

Color::Green.value # => 1

Each constant (member) in the enum has the type of the enum:

typeof(Color::Red) # => Color

Flags enum

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

@[Flags]
enum IOMode
  Read  # 1
  Write # 2
  Async # 4
end

Additionally, some methods change their behaviour.

Enums from integers

An enum can be created from an integer:

Color.new(1).to_s # => "Green"

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

Color.new(10).to_s # => "10"

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

Question methods

An enum automatically defines question methods for each member, using String#underscore for the method name.

For example:

color = Color::Blue
color.red?  # => false
color.blue? # => true

mode = IOMode::Read | IOMode::Async
mode.read?  # => true
mode.write? # => false
mode.async? # => true

This is very convenient in case expressions:

case color
when .red?
  puts "Got red"
when .blue?
  puts "Got blue"
end

Changing the Base Type

The type of the underlying enum value is Int32 by default, but it can be changed to any type in Int::Primitive.

enum Color : UInt8
  Red
  Green
  Blue
end

Color::Red.value # : UInt8

Included Modules

Defined in:

nason/to_json.cr

Constructors

Instance Method Summary

Instance methods inherited from struct Value

==(other : NASON::Any) ==

Instance methods inherited from class Object

===(other : NASON::Any) ===, nil_or_null? nil_or_null?, not_null! not_null!, null? null?, to_nason(io : IO) : Nil
to_nason : String
to_nason
, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil
to_pretty_json

Class methods inherited from class Object

from_nason(string_or_io, root : String)
from_nason(string_or_io)
from_nason

Constructor Detail

def self.new(pull : NASON::PullParser) #

Reads a serialized enum member by name from pull.

See #to_nason for reference.

Raises NASON::ParseException if the deserialization fails.


[View source]

Instance Method Detail

def to_nason(json : NASON::Builder) #

Serializes this enum member by name.

For non-flags enums, the serialization is a NASON string. The value is the member name (see #to_s) transformed with String#underscore.

enum Stages
  INITIAL
  SECOND_STAGE
end

Stages::INITIAL.to_nason      # => %("initial")
Stages::SECOND_STAGE.to_nason # => %("second_stage")

For flags enums, the serialization is a NASON array including every flagged member individually serialized in the same way as a member of a non-flags enum. None is serialized as an empty array, All as an array containing all members.

@[Flags]
enum Sides
  LEFT
  RIGHT
end

Sides::LEFT.to_nason                  # => %(["left"])
(Sides::LEFT | Sides::RIGHT).to_nason # => %(["left","right"])
Sides::All.to_nason                   # => %(["left","right"])
Sides::None.to_nason                  # => %([])

ValueConverter.to_nason offers a different serialization strategy based on the member value.


[View source]