One thing I really like about AS3 over AS2 is how much more compile-time type-checking it adds. However, it seems to be somewhat lacking in that there is no type-checked enumeration structure available. What's a good (best / accepted) way to do custom enumerated types in AS3?
Asked
Active
Viewed 2.6k times
5 Answers
17
your answer after the jump :-)

gltovar
- 463
- 4
- 10
-
while my edit is being reviewed I think this link would be similar to the one originally provided: [Enumerations with classes](http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f2f.html) – gltovar Feb 07 '11 at 21:19
15
1
I recently discovered that as3commons library has a good base helper class Enum for enums implemetation.

d9k
- 1,476
- 3
- 15
- 28
1
In order to be a true enum it needs to both:
- Enforce type safety
- Prevent rogue instances
Few of the simple solutions do both, and the base classes that do are overly complex IMO.
My current favourite is the following style - safe and simple, and shouldn't confuse anyone:
public final class FruitEnum {
private static const CREATE:Object = {};
public static const APPLE:FruitEnum = new FruitEnum(CREATE);
public static const ORANGE:FruitEnum = new FruitEnum(CREATE);
public static const BANANA:FruitEnum = new FruitEnum(CREATE);
public function FruitEnum(permission:Object) {
if (permission !== CREATE){
throw new Error("Enum cannot be instantiated from outside");
}
}
}
CAVEAT: I have seen rare circumstances where a variable initialisation reads an enum const before it set, but in those cases the problem applied equally to other const-based enum emulations.

Joachim Lous
- 1,316
- 1
- 14
- 22