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]
attribute. 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 an 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.
- In the case of regular enums, this compares by equality (
==
). - In the case of flags enums, this invokes
includes?
.
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
Included Modules
- Comparable(Enum)
Defined in:
to_con.crfrom_con.cr
Class Method Summary
Instance Method Summary
Instance methods inherited from struct Value
==(other : CON::Any)
==
Instance methods inherited from class Object
===(other : CON::Any)
===,
from_con(con : String | IO)
from_con,
to_con(io : IO)to_con to_con, to_pretty_con(indent : String = " ")
to_pretty_con(io : IO, indent : String = " ") to_pretty_con