Enumerated Types in ActionScript

| 7 Comments | 1 TrackBack

An enumerated type is a data type that maps names to numbers. At first this doesn't sound like a big deal, but enumerations can bring major readability benefits to code, making it easier to understand and therefore easier to maintain.

In my college days I coded mostly with C and C++. As such, I grew to love the enum keyword and the benefits that it provided. When I made the switch to ActionScript, I was a little disappointed to find that support for enumerations wasn't included in the language (although it's a future reserved word in ECMAScript 4). No worries though, it's pretty easy to fake with this handy little class.

/* A simple way to create enumerated types in ActionScript. Just provide a list of string arguments to the constructor and the strings will become identifiers in the instance. */ class com.darronschall.weblog.EnumeratedType { public function EnumeratedType() { for (var i = 0; i < arguments.length; i++) { // set up the identifier in the instance, and give // it a numerical value, unique to the instance this[arguments[i]] = i; } } }

You would use it in your scripts like this:

import com.darronschall.weblog.EnumeratedType; // create an enumeration for the days of the week days = new EnumeratedType("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); // the line below much easier to read than: // today = 4; // because the meaning of the code is clear // just by looking at it today = days.Thursday; // we can do neat things with enumerations as well, // like use them as loop counters for (var day = days.Sunday; day <= days.Saturday; day++) { if (day == today) { trace("Found a match.. day == today == days.Thursday"); } } // or even in a switch statement tomorrow = days.Friday; switch(tomorrow) { case days.Monday: trace("Monday.. back to work."); break; case days.Friday: trace("Alright! Tomorrow is Friday!"); break; }

Hopefully from the above example you can see some of the readability benefits that enumerations offer. Creating enumerations is remarkably easy with the class provided above. In the future, if the enum keyword is implemented, it will be even easier (in theory).

Have fun!

Further reading: http://www.alumni.caltech.edu/~leif/OO/Enum.html (Specifically on Enumerated Types in Java, but the concepts can be applied to ActionScript as well)

1 TrackBack

TrackBack URL: http://www.darronschall.com/mt/mt-tb.cgi/3

In my previous article, I covered ActionScript 3 languange syntax and constructs that may be of interest to C/C++ coders starting out on ActionScript 3. This article covers areas of AS3 that are different from C/C++ in regards to functional... Read More

7 Comments

  • It would great to find the enum keyword implemented in the next vesion of AS, as we found in the new Java 1.5b...

    Nice blog Darron : )

    C.

     
  • Hello!
    Off-topic comment: I didn't find any other email address to conctact you, so I'll use this form. I am setting up a blog about flash for portuguese speakers here in Brazil, and I've been looking all over for a code which makes my actionscripts citations color-coded... what do you use for this? Can you give me a hint, please?
    Thank you very much
    And keep up your excellent blog ! :)
    best regards
    Daniel

     
  • I use MTCodeBeautifier from Sean Voisen:

    http://voisen.org/archives/projects/000239.php

     
  • How about that:

    class com.darronschall.weblog.EnumeratedType {

    public function EnumeratedType() {
    for (var i = 0; i
    :)

     
  • How about that:

    class com.darronschall.weblog.EnumeratedType {

    public function EnumeratedType() {
    for (var i = 0; i < arguments.length; i++) {
    this[arguments[i]] = 1<< i;
    }
    }
    }

    (God damn < characters;)

     
  • Well this comes more than a year since the last post on this thread, but hopefully I may save someone from pulling their hair out like I did...

    I tried to use this class to create an Enum myself, but constantly got errors saying the dynamically created parameter didn't exist whenever I tried to reference one of the values in my new Enum.

    Oddly, I found I could reference the variable by going, for example:

    days["Monday"]

    But this is just an indexed array it seems.

    I searched and searched on ways to dynamically create variables, but none of the methods I could find would work, even those in the documentation.

    I should point at that I am using ActionScript 2.0 with an OO approach.

    Then I came across this page:
    http://labs.macromedia.com/wiki/index.php/ActionScript_3:Learning_Tips
    Which is actually a primer for ActionScript 3.0.

    But it mentions the following:

    Classes are sealed by default, meaning properties cannot be dynamically added at runtime.
    Classes can now be either dynamic or sealed. Dynamic classes can add additional dynamic properties at runtime; sealed classes cannot. Sealed classes conserve memory because no internal hashtable is needed, and the compiler can provide better error feedback. The declaration class Foo is sealed. To declare a class dynamic, use the dynamic keyword, i.e.dynamic class Foo.

    With nothing to lose, I added 'dynamic' to my class declaration, and sure enough, it now works fine!

    I'm not at all sure why this was an issue, 'dynamic' isn't even in the 2.0 documentation. I would love to hear from a more experienced Flash developer on this.

    Cheers,
    Chris.

     
  • Ahhh.

    Dynamic was indeed added in Flash 6. Only applies to ActionScript 2.0+, and only when the class is in an external script file.

    http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00001337.html

     

Leave a comment

Flex.org - The Directory for Flex

Archives