Enumerations
Enumerations are special classes consisting of zero or more variants.
enum Variant {
const VAR_ONE;
const VAR_TWO = "var_two";
const VAR_THREE = [2, "var_three"];
}
Note: Variable definitions within an
enumdefine static constants which are referred to as variants.
Final
Enumerations are final, so they cannot be extended.
Static
Enumerations are static, so they cannot be instantiated through the new operator.
Type inference
When the inference type in a string literal is an enumeration, the literal may identify a variant by its name.
var val : Variant = "var_one";
When the inference type in an array literal or object initializer is a flag enumeration, the literal may be used to identify multiple variants.
[Flags]
enum F { const A, B, C }
const m:F = ["a", "b", "c"];
// or
const m:F = { a: true, b: true, c: true };
Flag enumerations
Flag enumerations differ from regular enumerations by having instances being able to contain zero or more variants.
[Flags]
enum F { const A, B, C }
Flag enumerations may be assigned undefined, null or [] to indicate absence of variants.
When a flag enumeration’s numeric type is a floating point, the values are internally cast between a unsigned 32 bit integer and that floating point’s type; therefore their range may be less than what the floating point supports.
All variants
Obtain all variants of a flag enumeration by using the ** expression with the enumeration as the inference type:
var f:F = **;
Internation
Flag enumeration objects are interned so that flags may be compared correctly.
[Flags]
enum E { const A, B, C }
const obj:* = E(["a", "b"]);
trace(obj == E(["a", "b"]));
Customizing the numeric type
Enumerations use the uint type by default to represent the variant values. The user is allowed to change the type to another numeric type through using a meta-data named after that numeric type.
[decimal]
enum E1 {
const A, B, C;
}
Variant initializer
The initializer of a variant may be expressed in four different forms, or simply be omitted:
StringLiteral
NumericLiteral
[StringLiteral, NumericLiteral]
[NumericLiteral, StringLiteral]
The ArrayLiteral syntax is used to allow specifying both a string and a number.
Variant name
The variant name as declared by the const is determined as follows:
- Let r = empty string
- If the initializer does not contain a string literal
- Let orig = binding identifier name
- r = conversion of orig to lowercase.
- Else
- r = the value of the string literal at the initializer.
- If r is already used by another variant’s name
- Throw a verify error
- Return r
Variant value
The variant value as declared by the const is determined as follows:
- If the enumeration is a flag enumeration 2. Return DecideFlagValue()
- Return DecideValue()
DecideValue()
- Let r = zero
- If the initializer does not contain a numeric literal
- If there is no previous variant, return 0.
- Let k = previous variant’s value
- r = k + 1
- Else
- r = the value of the numeric literal at the initializer.
- If r is already used by another variant’s value
- Throw a verify error
- Return r
DecideFlagValue()
- Let r = zero
- If the initializer does not contain a numeric literal
- If there is no previous variant, return 1.
- Let k = previous variant’s value
- r = k * 2
- Else
- r = the value of the numeric literal at the initializer.
- If r is not one or a power of two
- Throw a verify error
- If r is already used by another variant’s value
- Throw a verify error
- Return r
Implicitly added methods
For all enumerations
valueOf()
override function valueOf():T {
//
}
Returns the numeric value of the enumeration instance, where T is the numeric type.
toString()
override function toString():string {
//
}
Returns the name of the enumeration instance. For a flag enumeration, returns the names of the enumeration instance delimited by comma (,) by ascending value order.
For flag enumerations
meta::has()
meta function has(v:E):boolean {
//
}
Returns a boolean indicating whether the instance contains the specified flags or not, where E is the enumeration itself.
This allows for f in e expressions.
with()
function with(v:E):E {
//
}
Returns a new value containing the specified flags, where E is the enumeration itself.
without()
function without(v:E):E {
//
}
Returns a new value removing the specified flags, where E is the enumeration itself.
toggled()
function toggled(v:E):E {
//
}
Returns a new value toggling the specified flags, where E is the enumeration itself.
Customized methods
Enumerations support customized methods:
enum E {
const A, B, C;
function get isA() this == "a";
}
Enumerations are prohibited from using variable definitions for purposes other than defining variants.