<div dir="ltr"><div dir="ltr"><div>Greetings,</div><div><br></div><div>I have been working on the problem of generic enums off and on for quite some time (see <a href="https://github.com/Mooninaut/cursed-generic-enum-java" target="_blank">https://github.com/Mooninaut/cursed-generic-enum-java</a> for a previous dead-end attempt).</div><div><br></div><div>I think I have an actual solution this time, and it doesn't require any changes to Java's type system. It only requires a slight tweak to the "extends" clause of generic enums: Replace "extends Enum<SomeEnum<T>>" with "extends Enum SomeEnum<?>>".</div><div><br></div><div>I have a fully-worked example of this at <a href="https://github.com/Mooninaut/java-generic-enums" target="_blank">https://github.com/Mooninaut/java-generic-enums</a> implementing the example given in <a href="https://mail.openjdk.org/pipermail/amber-spec-experts/2017-May/000041.html" target="_blank">https://mail.openjdk.org/pipermail/amber-spec-experts/2017-May/000041.html</a>, which was the stopping point for the original generic enums effort.</div><div><br></div><div>I included lightly modified copies of Enum, EnumSet and EnumSet's subclasses, and a Main class which demonstrates that the original problems with collections of enums are almost entirely resolved.</div><div><br></div><div>I also provide several generic enums with non-trivial type bounds to demonstrate that generic enums are no more limited than any other generic class.</div><div><br></div>I cannot say definitively that every conceivable generic type will work, but I haven't yet found any generic enum that wouldn't compile and run successfully.</div><div dir="ltr"><br></div><div>There is one slightly awkward issue. It's not possible to simply pass an enum class literal to EnumSet's static factory methods without a raw cast.</div><div><br>EnumSet.allOf((Class<Option<?>>) (Class) Option.class);</div><div><br></div><div>But this can be worked around by taking advantage of the fact that getDeclaringClass casts to Class<E></div><div><br></div><div>EnumSet.allOf(Option.D.getDeclaringClass());</div><div dir="ltr"><br></div><div>Cheers,</div><div>Clement Cherlin</div>
</div>