Home Contact

[August 4, 2005]

From C++ to ActionScript, Chapter 2: Doh! Enumerated Types Simplified

Filed under: ActionScript — @ 5:39 pm

In an earlier post I proposed a way to implement enumerated types as singleton classes in ActionScript, based on a single class written by Darron Schall, and showed a perl script to generate them. Well, I’ve realized it was unnecesarily complicated. All the values the class methods are returning are just integers, so why not just return them without bothering to create an instance?

Here’s a class that does the job with no instance and no variables:

class DaysOfWeek extends Object
{
public static function get Sunday ():Number { return 0; }
public static function get Monday ():Number { return 1; }
public static function get Tuesday ():Number { return 2; }
public static function get Wednesday ():Number { return 3; }
public static function get Thursday ():Number { return 4; }
public static function get Friday ():Number { return 5; }
public static function get Saturday ():Number { return 6; }

private function DaysOfWeek() {}
}

It’s no longer a Singleton. Maybe it’s a Zeroton, or a Noneton.

Now this implementation, like the previous ones, still has the flaw that its values are actually integers, and nothing keeps us from, say, adding 1 to Saturday and getting Octidi. But it will do for most applications. For a discussion of techniques for value restriction and type safety, see Implementing Enumerated Types in Java.

3 Comments »

  1. The original post you referenced from me was a long time ago, and better ways have been discovered since then. Check out Jason’s post here, and the discussion in the comments.

    http://blog.jasonnussbaum.com/?p=48

    Comment by darron — August 6, 2005 @ 11:50 pm

  2. Thanks, Darron, that’s indeed better!

    Comment by alan — August 8, 2005 @ 5:28 pm

  3. And way late…thanks, Darron/Alan.

    J.

    Comment by Jason Nussbaum — July 21, 2006 @ 5:21 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment


Contents copyright © Alan Shaw 2005-2008

25 queries. 0.268 seconds. Powered by WordPress version 2.5.1