Enums in AS3 / Flash / Flex?

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?


your answer after the jump :-)

Enumerations with classes

Just wanted to share my way

I recently discovered that as3commons library has a good base helper class Enum for enums implemetation.

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.

I know, this is a little outdated and does not exactly answer your question, but you might wanna check out Haxe. You can also use it to generate AS3 for you, plus there are many other reasons to use it. But this'd really get off topic...

Need Your Help

Why am I getting footprint FPMCP server errors

windows windows-xp

Why do I get footprint 4.8/FPMCP errors when I use iexplorer on my XP machine

"Not expecting A on this moment"

batch-file for-loop cmd

I'm doing a batch script to get some anime, I call livestreamer for that task. I'm trying to use this script