Two new draft pattern matching JEPs
Remi Forax
forax at univ-mlv.fr
Wed Mar 3 10:55:13 UTC 2021
----- Mail original -----
> De: "Gavin Bierman" <gavin.bierman at oracle.com>
> À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Jeudi 18 Février 2021 13:33:20
> Objet: Two new draft pattern matching JEPs
> Dear all,
>
> The next steps in adding pattern matching to Java are coming! I have drafted two
> new JEPs:
I'm slowly trying to wrap my head around these drafts
>
> - Nested Record and Array Patterns:
> https://bugs.openjdk.java.net/browse/JDK-8260244
For me the section Pattern Matching and record classes is missing a discussion about the fact that even if the pattern uses a binding,
the value of that binding is not accessed (so the corresponding record accessor is not called) until the binding is used at least once.
In
static void printXCoordOfUpperLeftPointWithPatterns(Rectangle r) {
if (r instanceof Rectangle(ColoredPoint(Point(var x, var y), var c), var lr)) {
System.out.println("Top-left Corner: " + x);
}
}
The bindings 'y', 'c' or 'lr' are not used so the corresponding record accessors are not called,
'x' is used so the accessors Rectangle.l(), ColoredPoint.p() and Point.x() are called.
I also found the code of the translation of "printXCoordOfUpperLeftPointBeforePatterns" confusing,
because the local variable 'c' in this example is not related to the binding 'c' in the pattern matching example.
static void printXCoordOfUpperLeftPointBeforePatterns(Rectangle r) {
if (r == null) {
return;
}
ColoredPoint c = r.l(); // here 'c' should be 'l' not 'c'
if (c == null) {
return;
}
Point p = c.p();
if (p == null) {
return;
}
int x = p.x();
System.out.println("Top-left Corner: " + x);
}
I also think that it will help the reader if record component names in the example are not identifiers with only one or two letters like in
record ColoredPoint(Point p, Color c) {}
record Rectangle(ColoredPoint ul, ColoredPoint lr) {}
While i think that like lambda, using identifiers with not a lot of letters is fine in a pettern, having classical identifiers for record component names will help to make the distinction between the component names and the binding names.
>
> Comments welcome as always!
>
> Thanks,
> Gavin
Rémi
More information about the amber-spec-experts
mailing list