Named record patterns
Brian Goetz
brian.goetz at oracle.com
Wed Apr 12 16:22:30 UTC 2023
I would like to eventually come back for named record patterns, but I think we have to let the dust settle first.
On Apr 11, 2023, at 2:48 PM, Remi Forax <forax at univ-mlv.fr<mailto:forax at univ-mlv.fr>> wrote:
________________________________
From: "Robbe Pincket" <robbepincket at live.be<mailto:robbepincket at live.be>>
To: "amber-dev" <amber-dev at openjdk.org<mailto:amber-dev at openjdk.org>>
Sent: Monday, April 10, 2023 1:27:22 PM
Subject: Named record patterns
Hi all
I was wondering if named record patterns `o instanceof Pair(String left, String right) pair` are still on the table for the future or not. (Not sure if 'named' record pattern is the right name?)
Let re-state what is the issue, currently there is no way to create a binding capturing the whole record instance, forcing people to transform the record pattern to a type pattern + when (see below).
A previous preview was supporting this case using the syntax
case Pair(String left, String right) pair -> ...
which did not work well in case someone want the binding to be also have an annotation or be declared final
Here is the previous syntax with "final"
case final Pair(String left, String right) pair -> ...
which is not very readable, the "final" being too far apart from the binding.
One solution is to use a keyword like "as" to add a binding at the end of a record pattern (and perhaps other patterns ?)
case Pair(String left, String right) as final pair -> ...
which as the advantage of grouping the binding and its keyword at the cost of adding a new keyword.
Rémi
IIRC they were shelfed due to the ambiguity that they could cause in this case, if there also exists a function `when`:
```
switch(o) {
case Pair(String left, String right) when when (left == right) -> ...
}
```
It feels weird to me however that this feature was shelved just for this case, when a 'simple' rule could be introduced that a record pattern can't introduce a pattern variable with the name of a contextual keyword or at least not `when`.
Are there other reasons that record patterns can't introduce a variable that matches the whole pair anymore? It feels like it could be useful, even more so with JEP 443: Unnamed Patterns and Variables (Preview)
```
if (o instanceof ColoredPoint(Point(_, int y), _) cp && y > 0) {
doAction(cp);
}
```
Instead of
```
if (o instanceof ColoredPoint(Point(int x, int y), Color c) && y > 0) {
doAction(new ColoredPoint(new Point(x, y), c));
}
```
or this, if ColoredPoint can both hold `Point` and `Point3D`
```
if (o instanceof ColoredPoint cp && cp.point() instanceof Point(_, int y) && y > 0) {
doAction(cp);
}
```
or if it can only hold `Point`
```
if (o instanceof ColoredPoint cp && cp.point().y() > 0) {
doAction(cp);
}
```
Greetings
Robbe Pincket
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20230412/4706c76e/attachment-0001.htm>
More information about the amber-spec-experts
mailing list