On Wed, 7 Jul 2021 12:10:16 GMT, Jan Lahoda <[email protected]> wrote:
>> Currently, an enum switch with patterns is desugared in a very non-standard,
>> and potentially slow, way. It would be better to use the standard
>> `typeSwitch` bootstrap to classify the enum constants. The bootstrap needs
>> to accept enum constants as labels in order to allow this. A complication is
>> that if an enum constant is missing, that is not an incompatible change for
>> the switch, and the switch should simply work as if the case for the missing
>> constant didn't exist. So, the proposed solution is to have a new bootstrap
>> `enumSwitch` that accepts `String`s in place of the enum constants, and will
>> internally convert them to the appropriate enum constants, and then it will
>> find the proper case similarly to `typeSwitch`.
>>
>> How does this look?
>
> Jan Lahoda has updated the pull request incrementally with one additional
> commit since the last revision:
>
> If the pattern type is a supertype of the selector type, use selector type
> rather than the pattern type when constructing the bootstrap method
> parameters.
Good catch - should we also add test for when an enum implements an interface?
enum Foo implements A { ... }
Foo foo = ...
switch(foo) {
case A a: ...
}
I think the patch you have should work since you are using subtyping.
Another thing to consider: should we be worried about type erasure turning the
selector type into some weird, unexpected type? I don't think that should be
the case, given that if a type variable has a bound E, where E is an enum, the
erasure should always be E (as E is a "class" type, so should win, even in
intersections).
-------------
Marked as reviewed by mcimadamore (Reviewer).
PR: https://git.openjdk.java.net/jdk17/pull/81