2.4 Enum Instance
Haxe provides powerful enumeration (short: enum) types, which are actually an algebraic data type (ADT). While they cannot have any expressions, they are very useful for describing data structures:
enum Color {
Red;
Green;
Blue;
Rgb(r:Int, g:Int, b:Int);
}
Semantically, this enum describes a color which is either red, green, blue or a specified RGB value. The syntactic structure is as follows:
- The keyword
enumdenotes that we are declaring an enum. Coloris the name of the enum and could be anything conforming to the rules for type identifiers.- Enclosed in curly braces
{}are the enum constructors, - which are
Red,GreenandBluetaking no arguments, - as well as
Rgbtaking threeIntarguments namedr,gandb.
The Haxe type system provides a type which unifies with all enum types:
Define:
Enum<T>This type is compatible with all enum types. At compile-time,
Enum<T>can bee seen as the common base type of all enum types. However, this relation is not reflected in generated code.