title: Java enums and generics
body: <p>This thing is troubling me for a while now. I have asked <a href="https://stackoverflow.com/questions/4290878/why-shouldnt-java-enum-literals-be-able-to-have-generic-type-parameters">questions</a> before, but probably with a bad phrasing and an example that was too abstract. So it wasn't clear what I was actually asking. I'll try again. And please don't jump to conclusions. I expect that the question is not easy at all to answer!</p>
  
  <p><strong>why can't I have an enum with generic type parameters in Java?</strong></p>
  
  <p>The question is not about why it's not possible, syntactically. I know it's just not supported. The question is: why did the JSR people "forget" or "omit" this very useful feature? I can't imagine a compiler-related reason, why it wouldn't be feasible. </p>
  
  <p>Here's what I would love to do. This is possible in Java. It's the Java 1.4 way to create typesafe enums:</p>
  
  <pre><code>// A model class for SQL data types and their mapping to Java types
  public class DataType&lt;T&gt; implements Serializable, Comparable&lt;DataType&lt;T&gt;&gt; {
      private final String name;
      private final Class&lt;T&gt; type;
  
      public static final DataType&lt;Integer&gt; INT      = new DataType&lt;Integer&gt;("int", Integer.class);
      public static final DataType&lt;Integer&gt; INT4     = new DataType&lt;Integer&gt;("int4", Integer.class);
      public static final DataType&lt;Integer&gt; INTEGER  = new DataType&lt;Integer&gt;("integer", Integer.class);
      public static final DataType&lt;Long&gt;    BIGINT   = new DataType&lt;Long&gt;   ("bigint", Long.class);    
  
      private DataType(String name, Class&lt;T&gt; type) {
          this.name = name;
          this.type = type;
      }
  
      // Returns T. I find this often very useful!
      public T parse(String string) throws Exception {
          // [...]
      }
  
      // Check this out. Advanced generics:
      public T[] parseArray(String string) throws Exception {
          // [...]
      }
  
      // Even more advanced:
      public DataType&lt;T[]&gt; getArrayType() {
          // [...]
      }
  
      // [ ... more methods ... ]
  }
  </code></pre>
  
  <p>And then, you could use <code>&lt;T&gt;</code> in many other places</p>
  
  <pre><code>public class Utility {
  
      // Generic methods...
      public static &lt;T&gt; T doStuff(DataType&lt;T&gt; type) {
          // [...]
      }
  }
  </code></pre>
  
  <p>But these things are not possible with an enum:</p>
  
  <pre><code>// This can't be done
  public enum DataType&lt;T&gt; {
  
      // Neither can this...
      INT&lt;Integer&gt;("int", Integer.class), 
      INT4&lt;Integer&gt;("int4", Integer.class), 
  
      // [...]
  }
  </code></pre>
  
  <p>Now, as I said. I know these things have been designed exactly that way. <code>enum</code> is syntactic sugar. So are generics. Actually, the compiler does all the work and transforms <code>enums</code> into subclasses of <code>java.lang.Enum</code> and generics into casts and synthetic methods.</p>
  
  <p><strong>but why can't the compiler go further and allow for generic enums??</strong></p>
  
  <p><strong>EDIT</strong>:
  This is what I would expect as compiler-generated Java code:</p>
  
  <pre><code>public class DataType&lt;T&gt; extends Enum&lt;DataType&lt;?&gt;&gt; {
      // [...]
  }
  </code></pre>