[code-reflection] RFR: Support match all pattern
Paul Sandoz
psandoz at openjdk.org
Mon Sep 16 20:03:19 UTC 2024
On Mon, 16 Sep 2024 07:47:21 GMT, Mourad Abbay <mabbay at openjdk.org> wrote:
> To lower MatchAllPatternOp, we don't need to do anything, we just skip it, there are no bindings.
It's not that simple :-)
The JLS states [here](https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-14.30.1):
Let R be the type of the record pattern r, and let T be the type of the corresponding component field in R
([§8.10.3](https://docs.oracle.com/javase/specs/jls/se22/html/jls-8.html#jls-8.10.3)). The type of the match-all
pattern is the upward projection of T with respect to all synthetic type variables mentioned by T.
A record's component still needs to be accessed - its result is not bound to a named variable. e.g., consider:
record R(Number s) {
public Number s() {
if (s instanceof Integer) {
throw new IllegalArgumentException();
}
return s;
}
}
R r = new R(1);
if (r instanceof R(_)) {
System.out.println("");
}
The JLS states [here](https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-14.30.2):
A value v that is not the null reference matches a record pattern with type R and component pattern list L if (i) v can be
converted by testing conversion ([§5.7](https://docs.oracle.com/javase/specs/jls/se22/html/jls-5.html#jls-5.7)) to the target
type R without raising a ClassCastException; and (ii) each record component of v matches the corresponding component
pattern in L; and does not match otherwise.
Each record component of v is determined by invoking the accessor method of v corresponding to that component. If
execution of the invocation of the accessor method completes abruptly for reason S, then pattern matching completes
abruptly by throwing a MatchException with cause S.
-------------
PR Comment: https://git.openjdk.org/babylon/pull/230#issuecomment-2353804076
More information about the babylon-dev
mailing list