Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy shouldn't Java enum literals be able to have generic type parameters?
    text
    copied!<p>Java enums are great. So are generics. Of course we all know the limitations of the latter because of type erasure. But there is one thing I don't understand, Why can't I create an enum like this:</p> <pre><code>public enum MyEnum&lt;T&gt; { LITERAL1&lt;String&gt;, LITERAL2&lt;Integer&gt;, LITERAL3&lt;Object&gt;; } </code></pre> <p>This generic type parameter <code>&lt;T&gt;</code> in turn could then be useful in various places. Imagine a generic type parameter to a method:</p> <pre><code>public &lt;T&gt; T getValue(MyEnum&lt;T&gt; param); </code></pre> <p>Or even in the enum class itself:</p> <pre><code>public T convert(Object o); </code></pre> <h3>More concrete example #1</h3> <p>Since the above example might seem too abstract for some, here's a more real-life example of why I want to do this. In this example I want to use</p> <ul> <li>Enums, because then I can enumerate a finite set of property keys</li> <li>Generics, because then I can have method-level type-safety for storing properties</li> </ul> <pre><code>public interface MyProperties { public &lt;T&gt; void put(MyEnum&lt;T&gt; key, T value); public &lt;T&gt; T get(MyEnum&lt;T&gt; key); } </code></pre> <h3>More concrete example #2</h3> <p>I have an enumeration of data types:</p> <pre><code>public interface DataType&lt;T&gt; {} public enum SQLDataType&lt;T&gt; implements DataType&lt;T&gt; { TINYINT&lt;Byte&gt;, SMALLINT&lt;Short&gt;, INT&lt;Integer&gt;, BIGINT&lt;Long&gt;, CLOB&lt;String&gt;, VARCHAR&lt;String&gt;, ... } </code></pre> <p>Each enum literal would obviously have additional properties based on the generic type <code>&lt;T&gt;</code>, while at the same time, being an enum (immutable, singleton, enumerable, etc. etc.)</p> <h3>Question:</h3> <p>Did no one think of this? Is this a compiler-related limitation? Considering the fact, that the keyword "<strong>enum</strong>" is implemented as syntactic sugar, representing generated code to the JVM, I don't understand this limitation.</p> <p>Who can explain this to me? Before you answer, consider this:</p> <ul> <li>I know generic types are erased :-)</li> <li>I know there are workarounds using Class objects. They're workarounds.</li> <li>Generic types result in compiler-generated type casts wherever applicable (e.g. when calling the convert() method</li> <li>The generic type &lt;T&gt; would be on the enum. Hence it is bound by each of the enum's literals. Hence the compiler would know, which type to apply when writing something like <code>String string = LITERAL1.convert(myObject); Integer integer = LITERAL2.convert(myObject);</code></li> <li>The same applies to the generic type parameter in the <code>T getvalue()</code> method. The compiler can apply type casting when calling <code>String string = someClass.getValue(LITERAL1)</code></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload