Named record patterns

Robbe Pincket robbepincket at live.be
Mon Apr 10 11:27:22 UTC 2023


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?)

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-dev/attachments/20230410/074b4911/attachment-0001.htm>


More information about the amber-dev mailing list