HaxeDoc2

(やまだぃちぅ) #1
Type: EnumValue
EnumValue is a special type which unifies with all enum instances. It is used by the Haxe
Standard Library to provide certain operations for all enum instances and can be employed
in user-code accordingly in cases where an API requiresanenum instance, but not a specific
one.

It is important to distinguish enum types and enum constructors, as this example demon-
strates:
1 enum Color {
2 Red;
3 Green;
4 Blue;
5 Rgb(r:Int, g:Int, b:Int);
6 }
7
8 class Main {
9 static public function main() {
10 var ec:EnumValue = Red; // valid
11 var en:Enum=Color; //valid
12 // Error: Color should be Enum
13 //var x:Enum = Red;
14 }
15 }


If the commented line is uncommented, the program does not compile becauseRed(an enum
constructor) cannot be assigned to a variable of typeEnum<Color>(an enum type). The relation
is analogous to a class and its instance.

Trivia: Concrete type parameter forEnum<T>
One of the reviewers of this manual was confused about the difference betweenColorand
Enum<Color>in the example above. Indeed, using a concrete type parameter there is point-
less and only serves the purpose of demonstration. Usually we would omit the type there
and let type inference (3.6) deal with it.
However, the inferred type would be different fromEnum<Color>. The compiler infers a
pseudo-type which has the enum constructors as “fields”. As of Haxe 3.2.0, it is not possible
to express this type in syntax but also, it is never necessary to do so.

2.4.2 Using enums


Enums are a good choice if only a finite set of values should be allowed. The individual construc-
tors (2.4.1) then represent the allowed variants and enable the compiler to check if all possible
values are respected. This can be seen here:
1 enum Color {
2 Red;
3 Green;
4 Blue;
5 Rgb(r:Int, g:Int, b:Int);
6 }
7
Free download pdf