Nice TWiki > Dev > NiceDiscussions? > EnumImplementation (r1.4) 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

We can use dispatch on value for enum by giving each element a final field(named ordinal?). It is only a bit more work.

Another implementation problem is how do we prevent the addition of elements after the enum is made. It should not be possible because it might bypass the coverage tests.

-- ArjanB

An important question is: should enums be extendible? It's tricky because then a inverted inheritance tree for enums is needed. I think it's doable although I haven't figured out the details yet.

Because of the complexity for the users and of the implementation, Java 1.5 will not have extendible enums.

-- ArjanB - 16 May 2003

Topic EnumImplementation . { Edit | Attach | Ref-By | Printable | Diffs | r1.8 | > | r1.7 | > | r1.6 | More }
Revision r1.4 - 16 May 2003 - 00:47 GMT - ArjanB
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.