Nice TWiki > Dev > NiceDiscussions? > EnumImplementation (r1.2) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Dev . { Changes | Index | Search | Go }
Nice will get enums soon but first the exact implementation and syntax needs to be discussed.


A good starting point is the java 1.5 enum proposal:

An example of the java proposal:

public enum Coin {
  penny(1), nickel(5), dime(10), quarter(25);
  Coin(int value) { this.value = value; }

  private final int value;
  public int value() { return value; }
In Nice we could make the syntax simpler:
public enum Coin(int value) { penny(1), nickel(5),  dime(10),  quarter(25)}


And the compiler would generate in case of this example something like:

abstract class Coin implement Enum {
final Coin penny = new penny();
final Coin nickel = new nickel();
final Coin dime = new dime();
final Coin quarter = new quarter();
final class penny extends Coin{}
final class nickel extends Coin{}
final class dime extends Coin{}
final class quarter extends Coin{}

int value(Coin);
value(f@penny) = 1;
value(f@nickel) = 5;
value(f@dime) = 10;
value(f@quarter) = 25;

-- ArjanB - 30 Apr 2003

An alternative is not to declare a class for each value, but make them an instance of the same class. An advantage is that we generate less classes, which should improve runtime efficiency (loading classes takes time to the jvm). If we did that, then we cannot use class dispatch for methods. We could either compare references, or assign a unique small number to each case, and use a switch bytecode to dispatch. This is an implementation detail, and should not make any difference in the specification part. This makes it possible to use the class implementation first since it seems simpler, and to optimize for speed after the implementation works well.

-- DanielBonniot - 30 Apr 2003

Topic EnumImplementation . { Edit | Attach | Ref-By | Printable | Diffs | r1.8 | > | r1.7 | > | r1.6 | More }
Revision r1.2 - 30 Apr 2003 - 10:05 GMT - DanielBonniot
Parents: NiceDiscussions?
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.