#64 Enums

brian Mon 17 Apr 2006

I checked in support for enums. My intention is to support a full syntax very similar to Java enums with special constructor support and constant-specific methods (http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html). For now I have basic support with the ability to add additional slots:

enum Suit
{
  clubs,
  diamonds,
  hearts,
  spades

  Bool isBlack() { return this == clubs || this == spades }

  static Void main()
  {
    values.each |Suit s|
    {
      echo(s + " black=" + s.isBlack)
    }
  }
}

You can access the integer ordinal via the ordinal() method and the programmatic identifier via name(). Plus every enum will provide a static field called values with a list of it's range. The main method above illustrates iterating the values of the enum.

brian Mon 17 Apr 2006

Actually, I finished up support for enum constructors since it was easy to implement and quite handy:

enum Suits
{
  clubs("black"),
  diamonds("red"),
  hearts("red"),
  spades("black");

  new make(Str color) { this.color = color; }

  Str color;
}

Login or Signup to reply.