As a small elaboration of Barney B’s typesafe enumeration example, I have built an abstract class on which to base AS3 enumerations:
package com.almirun.common.util {
import flash.utils.getQualifiedClassName;
public class Enumeration {
protected static var enumLock:Boolean = false;
public function Enumeration() {
if (enumLock) {
throw new Error("Runtime instantiation of " +
getQualifiedClassName(this) +
" is disallowed because it is a typesafe enumeration");
}
}
}
}
Some caveats: AS3 does not actually support abstract classes; the abstract nature of the class is therefore implicit and unenforced. Also, you have to include the static block in the subclass, e.g.:
package com.almirun.examples {
public class Fruit extends Enumeration {
public static const APPLE:Fruit
= new Fruit(1, "Crisp and crunchy, good for making cider");
public static const ORANGE:Fruit
= new Fruit(2, "Juicy, sweet and a little acidic");
// prevent new instances from being created at runtime
{
enumLock = true;
}
private var _id:int;
public function get id():int {
return _id;
}
private var _description:String;
public function Fruit(id:int, description:String) {
super();
_id = id;
_description = id;
}
public function toString():String {
return "Fruit: " + _description;
}
public function equals(o:Object):Boolean {
return o is Fruit && (o as Fruit).id == _id;
}
}
}
… so, extending Enumeration does not, of itself, guarantee type safety. But, given that we’re dependent on a hacky technique for getting this going at all, it’s a reasonable halfway.
[Note: posting code in Wordpress and retaining the indentation is about as easy as having a tooth pulled.]


[...] earlier post with code for an abstract class on which to base typesafe enumerations was [...]