Why don't enums allow generics since Java 5? (was "Re: 2 questions about enums and the closed/withdrawn JEP 301")

David Alayachew davidalayachew at gmail.com
Thu Mar 30 02:40:45 UTC 2023


Whoops, forgot to add the link. Here it is.

[1] =
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Enum.java#L65

On Wed, Mar 29, 2023 at 10:39 PM David Alayachew <davidalayachew at gmail.com>
wrote:

> Hello Maurizio,
>
> Thank you for your response!
>
> > sadly I don’t know the answer to your question
> > as I wasn’t around when enums were added to the
> > language ;-)
>
> Lol, fair enough. I actually poked around the enum class itself [1] and
> saw that it was originally made by at least 2 folks - Josh Bloch and Neal
> Gafter. Maybe they read these mailing lists on occasion?
>
> > I agree with all Brian said. I’d also add that
> > the failure modes we discovered when we worked
> > on generic enums are rather obscure, as they
> > are a result of *three* factors: (i) you need to
> > have some class Foo that is transitioning from
> > being non-generic to being generic and (ii) you
> > have to have some existing utterances of Foo
> > nested inside some other parameterized type
> > (e.g. Box<Foo>) and (iii) the declaration of
> > that second generic class has to be
> > sufficiently strict (e.g. Box<X extends Foo<X>>).
> > If *all* these conditions are met, then you end
> > up with cases where utterances of Box<Foo>
> > were valid before generification of Foo, but
> > invalid afterwards. Something like this was
> > (and still is!) pretty hard to spot.
>
> Thank you so much for the context. This really clarifies a lot and helps
> me to understand even more the hesitation to push forward. Not only is it
> going to break code, but the scope at which it will is difficult to
> measure, so we can't accurately judge the cost to the benefit.
>
> > One might argue (given what we know now) that
> > it would have been perhaps better not to make
> > the Enum class itself generic. But the static
> > type checking you get on stuff like
> > EnumSet.noneOf(Foo.class) is hard to pass up: if
> > Enum wasn’t a generic class, then there’d be no
> > way to tie the type of the class literal to the
> > type of the returned EnumSet. So I think the
> > Enum generic declaration we ended up with is the
> > result of a forced move (type-system wise).
>
> One thing being on this mailing list has taught me is that it is all about
> tradeoffs when trying to figure out whether or not to put stuff into the
> language, let alone how. I definitely see what you mean now.
>
> Thank you for the insight and context!
> David Alayachew
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230329/f4f0bf93/attachment-0001.htm>


More information about the amber-dev mailing list