Named record pattern

Tagir Valeev amaembo at gmail.com
Mon May 30 16:36:12 UTC 2022


Hello!

I'm reading the spec draft near "14.30.1 Kinds of Patterns" [1] and I
wonder how the variable declared as named record pattern differs from
the variable declared in the type test pattern

Assuming record Point(int x, int y) {}

One can use a pattern like
  obj instanceof Point p
or use a pattern like
  obj instanceof Point(int x, int y) p
It looks like the variable 'p' should be quite similar in both cases. However:
- In the first case we are free to declare 'p' as final or not. In the
second case it's unclear from the spec whether the variable 'p' is
final or not and whether the user has control on this. It looks like,
"obj instanceof final Point(int x, int y) p" syntax is not allowed
which brings some asymmetry
 - In the first case I can use LOCAL_VARIABLE annotations like 'obj
instanceof @Cartesian Point p'. It looks like I cannot do the same in
the second case, which is another asymmetry.

So if I want to upgrade the type test pattern on a record type to a
record pattern to match components, I need to give up some features
like finality and annotations. Is this intended?

With best regards,
Tagir Valeev

[1] https://cr.openjdk.java.net/~gbierman/PatternSwitchPlusRecordPatterns/PatternSwitchPlusRecordPatterns-20220407/specs/patterns-switch-jls.html#jls-14.30.1


More information about the amber-spec-experts mailing list