2 questions about enums and the closed/withdrawn JEP 301
Archie Cobbs
archie.cobbs at gmail.com
Wed Mar 29 18:33:48 UTC 2023
On Wed, Mar 29, 2023 at 12:53 PM Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:
> The following is legal:
>
> Box<? extends Foo>
>
> But that is also a type that has significantly different characteristics
> from the ones above - e.g. you can’t call Box::set on it. So, even before
> getting to generic methods accepting Foos, we have a much more fundamental
> problem: we can't even express the type of a variable of type Box<Foo> in
> the type-system (!!).
>
Thanks Maurizio... and of course if I had actually tried what I was
suggesting I would have seen the problem... !
You *can* come close though... you just have to pay for your offense with
@SuppressWarnings("unchecked"):
import java.util.*;
public class GenericEnum {
public static class Enum<T extends Enum<T>> {
}
public static class Foo<S> extends Enum<Foo<S>> {
}
public abstract static class EnumSet<T extends Enum<T>> implements
Set<T> {
public static <T extends Enum<T>> EnumSet<T> noneOfGeneric(Class<?
extends T> c) {
return null;
}
}
@SuppressWarnings("unchecked") // this is still necessary, too bad
public static void main(String[] args) {
Set<Foo<?>> fooSet = EnumSet.noneOfGeneric(Foo.class);
}
}
Personally, I would still be happy to have generic enums even without any
accomodation by EnumSet/EnumMap.
Then all I would have to homebrew would be a few wrapper methods around the
EnumSet/EnumMap factory methods :)
-Archie
--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230329/40086547/attachment.htm>
More information about the amber-dev
mailing list