RFR: 8262889: Compiler implementation for Record Patterns
Jan Lahoda
jlahoda at openjdk.java.net
Fri May 6 10:54:44 UTC 2022
On Thu, 5 May 2022 15:17:11 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> You are right. It is the ii) which iteratively checks the component pattern list L.
>
> 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`.
-------------
PR: https://git.openjdk.java.net/jdk/pull/8516
More information about the core-libs-dev
mailing list