New candidate JEP: 440: Record Patterns

David Alayachew davidalayachew at gmail.com
Sat Mar 18 06:07:45 UTC 2023


Hello Amber Dev Team,

In the Record patterns section of the link (little more than halfway down).
It says the following quote.

---

The null value does not match any record pattern.

---

I understand that this is relatively clear and simple enough to understand,
but in the name of explicitness and beginner-friendlyness, could we add
something like this? I understand that it is redundant, but this extra
effort will help uproot a lot of the misinformation that people will
inevitably come up with.

---

The null value does not match any record pattern. For example, the
following code will print "No match".

record Point(Integer x, Integer y) {}
enum Color { RED, GREEN, BLUE; }
record ColoredPoint(Point p, Color c) {}

ColoredPoint cp = null;

if (cp instanceof ColoredPoint(Point(Integer x, Integer y), Color c)) {
    System.out.println("Match 1! x = " + x + " and y = " + y + " and c = "
+ c);
} else if (cp instanceof ColoredPoint(Point p, Color c)) {
    System.out.println("Match 2! p = " + p + " and c = " + c);
} else {
    System.out.println("No match");
}

However, if we change cp to be the following

cp = new ColoredPoint(null, null);

then the output would be "Match 2! p = null and c = null".

And if we change cp to be the following

cp = new ColoredPoint(new Point(null, null), null);

then the output would be "Match 1! x = null and y = null and c = null"

---

I understand that it feels excessive, but this level of clarity and
explicitness makes reasoning about the rest of the JEP a lot easier. null
is always at the back of my mind, so having explicit examples of how it
plays with the new features clears up the ambiguity and makes understanding
the new features that much easier.

Thank you for your time!
David Alayachew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230318/468748d8/attachment.htm>


More information about the amber-dev mailing list