4
Apr
2006

AS3 technique — using object instances as “enums”

Just after posting the updated version of the CompletionInput control, it occured to me that I should read through the source code to make sure there’s nothing too strange lurking in there.

One thing that might warrant some explanation is why I used object instances to represent “enums”. This is a technique that should be familiar to Java developers but may not be familiar to all AS developers.

~

In LoopResult.as, I define constants like this:

public class LoopResult {
    public static const KEEP_GOING : LoopResult = new LoopResult();
    public static const STOP : LoopResult = new LoopResult(); 
}

Whereas in CompletionInput.as, I define them like this:

public class CompletionInput extends mx.controls.TextInput 
{
    public static const COMPLETION_FAILED    : int = 1;
    public static const COMPLETION_SUCCEEDED : int = 2;
    public static const COMPLETION_ASYNC     : int = 3;
    ...
}

In both cases, I use these static members as if they were “enums”.

	...
	return LoopResult.KEEP_GOING;
or
	return CompletionInput.COMPLETION_SUCCEEDED;

The main difference between the two is that the first version is typesafe — I can declare my function as returning a LoopResult, not an int or a String.

PRO:

  • Using object instances for enumerations makes your program more typesafe. This can be a huge win!

CON:

  • More complex — you have to create another class to hold your enumeration type.
  • Somewhat less readable.
  • Possible memory or performance issue, although I bet it’s negligible.

10 Responses to “AS3 technique — using object instances as “enums””

  1. Peter

    This technique is particularly nice in AS3, compared to Java, because AS3 lets you switch on any type. But I’d still consider adding a string property to the class, for use in debugging.

  2. Campbell

    Yeah cool,
    I have been doing similar, thought id try it after using the internal struc & enum class types in c#.

    Cam

  3. Jason Nussbaum

    I blogged something like that for AS 2 way back…
    http://blog.jasonnussbaum.com/?p=48

    With some added toString methods and all that jazz…

  4. Sho

    Peter: Great comments. I hadn’t thought about the AS3 “switch on any type” aspect.

    Jason: Glad to see we’re on the same page. Thanks for the link.

  5. Jens Halm

    But in Java 5 you can at least switch on “real” enums.

    Btw: If only Adobe would not prevent private and protected constructors, it would make life so much easier for creating singletons and faking enums and abstract classes. Still don’t know why they are not allowed any more…

  6. Sho

    I’m glad that Java 5 has enums and generics. Maybe we can get them into AS4? :-)

    I started writing a reply here, but it ended up getting quite long so I’m moving it to a new entry. Let me know what you think.

  7. ivan

    The above approach still isn’t quite “typesafe” since you can use “new LoopResult()” instead of one of the consts, which would pass compilation but would break the code at runtime. If constructors were allowed to be declared private, then it’d be less of a kludge. Bottom line — AS needs proper enums.

  8. Paco

    The TypeSafe method is not really useful when you want to save this information into a database. I would prefer to save a number.

    A number is also easier to transport between the various software of a system. From flex program to businessObject to database.

    I want to ask for a specific enumeration type in my function parameter like the TypeSafe allow me, but I don’t want to save this object into my database. I want to save a byte no more!

    Why they never build a real Enumeration structure in AS# !?

  9. Troy Gardner

    Paco: the enum object is a full blown class so can have whatever id/strings you want to serialize, and as many methods as you want to marshal bac and forth (e.g. toXMLString(), fromXMLString());

    Sho:
    I profiled use of String/int,uint, number in switch statements and using ‘enum’s was fastest by 1/3 of the String time when you cache a refence locally. The reason for this is it uses identity (===) rather than value comparisons (==). I cache a copy. Here’s some sample code

    public static const A:MyEnum = MyEnum.A;

    ….
    switch(someEnum){
    case A:
    break;
    case B:
    }

  10. Samuel Asher Rivello

    Enums are my favorite community developed language feature!!!

    An Enum is a kind of variable that is not natively available in ActionScript 3.0, but can be found in other languages such as C++. An enum is a type with a restricted set of values.

    Here is a demo;
    http://www.blog.rivello.org/?p=621

Leave a Reply