RFR: 8262889: Compiler implementation for Record Patterns
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Fri May 6 11:47:46 UTC 2022
On Fri, 6 May 2022 10:51:33 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
>> I now believe that the check is needed to properly classify patterns based on the type of the i-th component. That said, not sure this should be a subtyping check, or a type equality
>
> A good question. Consider code like:
>
> private void test(R r) {
> switch (r) {
> case R(A a, A v) -> {}
> case R(B b, A v) -> {}
> case R(I i, B v) -> {}
> }
> }
> final class A implements I {}
> sealed interface I permits A, B {}
> final class B implements I {}
> record R(I i1, I i2) {}
>
>
> The switch is exhaustive - all the possible combinations are covered. When checking the first component, the code will categorize the patterns like:
>
> A -> R(A a, A v), R(I i, B v)
> B -> R(B b, A v), R(I i, B v)
> I -> R(I i, B v)
>
> this categorization is done using the subtype check, so that `R(I i, B v)` will appear in the list for `A`. When checking the second component, the possibility for `I` is not exhaustive (does not cover `A` in the second component), but the possibilities for `A` and `B` are exhaustive, and they together cover `I`.
Ah - makes sense of course - I "just" needed a more convoluted example :-)
-------------
PR: https://git.openjdk.java.net/jdk/pull/8516
More information about the core-libs-dev
mailing list