Update: Faking Enums in AS3
[Update: the final version of my enum class is here.]
Cardin posted a comment (thanks!) with an update to Faking Enums in AS3, and rather than post a comment reply, I figured I’d do a new post instead. Then I can get syntax highlighting.
I did some tests in Flex and unfortunately, this simplification won’t work. A couple problems arise.
First, if you don’t init those constants in EState and just leave them unassigned, then the Flex compiler gives a warning for each with a little hazard icon next to each constant. Not is this annoying, for me it’s a deal-breaker. I insist on having warning-clean projects. Warnings are so useful for catching bugs, that I always set them to the max. Also, if an option is available on the compiler I’m using to turn warnings into errors, then I always set it to make sure those warnings get fixed. We do the same at my work. Catches lots of great stuff.
So let’s just assign each of those constants to null. It’s not ideal but it is still a bit simpler than ‘new EState()’. Now we run into the second problem. The VM refuses to let you modify those constants. This was actually a pleasant surprise for me. I’m so accustomed to older versions of ActionScript letting you do whatever sloppy stuff you want. Powerful but dangerous in the long run. The new compiler throws an exception:
ReferenceError: Error #1074: Lectura no permitida de la propiedad Ready de sólo lectura en class EState.
at EEnum$/initEnum()[C:\Users\sbilas\Proj\tt\main\test-flex\src\EEnum.as:14]
Sorry it’s in español, but that’s how I have my machine configured (me gusta practicar). Roughly translated it says that writing isn’t permitted from the read-only property. Well actually it says that reading isn’t permitted but I’m pretty sure that’s a translation bug (should be “escritura”). I tested with a trace statement and reading works just fine.
Anyway, the exception happens when trying to assign in that “new inType()”. So the “= new EState() can’t be left out for each constant, unfortunately, as far as I can tell. We could remove the ‘const’ but that’s starting to sacrifice safety for a small amount of programmer convenience. The enum is going to be used far more often than it will be written, so we’ll leave the const in there.
However, there is one tiny improvement I was able to make, and it looks like this:
import flash.utils.describeType; public class EEnum { public var Text :String; protected static function initEnum(i_type :*) :void { var type :XML = flash.utils.describeType(i_type); for each (var constant :XML in type.constant) { i_type[constant.@name].Text = constant.@name; } } } public class EState extends EEnum { // static ctor {initEnum(EState);} public static const Initializing :EState = new EState(); public static const Connecting :EState = new EState(); public static const Loading :EState = new EState(); public static const Ready :EState = new EState(); }
So, we have a base class to extend that holds the Text field, and we might as well move the constant init code into the base as well instead of a general utility class. It’s probably only ever going to be used in this way anyway.
Overall this is a bit simpler now. This reduces the requirements for an enum to three fairly minimal things:
- Extend EEnum.
- Call initEnum(EnumType) in a static ctor.
- Set all enum “constants” as public static consts assigned to a new EnumType().
If you can get it any smaller than this, then please post a comment!


Yikes. I was using my “solution” for a project I was working on, but haven’t actually gone far enough to test-compile it. o.o Apologies on that error.
Cardin
11 Dec 08 at 10:33 pm
I’m probably missing something fundamental, but why not just do something like this:
public class SeriesType
{
public static const LINE_SERIES:SeriesType = new SeriesType(“LineSeries”);
public static const DOT_SERIES:SeriesType = new SeriesType(“DotSeries”);
public var _type:String;
public function SeriesType(type:String){
_type = type;
}
}
That gives you type safety, and when you’re debugging you can just look at the _type member of the Enum object.
Ben
Ben
13 Jan 09 at 7:19 pm
Ben – that does give you type safety, but there’s still some duplication required across enum types. It’s more verbose than it needs to be and still has some potential for error. For example, you can put whatever you want in the class, whereas I want to require only enum “constants” appear there.
Check out the last update I made to this article here: http://scottbilas.com/2008/12/16/update-2-faking-enums-in-as3/. It requires a minimal amount of code per enum, and solves all the remaining type issues.
Scott
8 Feb 09 at 1:10 pm
[...] last revision I’m going to make to my fake AS3 enum class (here’s the original posting, then two updates). This is inspired by commenter Mikko Korpela, who asked about an iteration ability. Well, [...]
Ultimate AS3 Fake Enums at New Fun Blog – Scott Bilas
25 Dec 09 at 1:15 pm