Named record pattern
Brian Goetz
brian.goetz at oracle.com
Mon May 30 16:53:55 UTC 2022
Thank you so much for catching this.
On 5/30/2022 12:36 PM, Tagir Valeev wrote:
> 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.
I must admit to being very surprised that you can do this at all! I
don't recall discussion on this, and had you asked me, I would have said
that `final` has no place in type-test patterns. Yet, I just tried it
with jshell and it does work as you say. I am surprised.
Can someone recall any discussion over this? (Surely now someone will
point me to where I agreed to this.)
Worse, it even works in switch labels! This is definitely not what I
had in mind. Did this happen because we reused the local variable
production for type patterns? Since switch patterns are about to exit
preview, I think we need to fix this ASAP, before switch exits preview.
> 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
This very question is why I would not have encouraged us to try to do
this for type test patterns at all!
> instanceof @Cartesian Point p'. It looks like I cannot do the same in
> the second case, which is another asymmetry.
We definitely intended to not allow declaration annotations. As to
type-use annotations; well, that's a different problem, and I'm not
quite sure what to do. For sure, we are not going to amend the
XxxTypeAnnotations attributes to reify the position of these
annotations. If we allow them and make them available only to
annotations processors only, that's another kind of asymmetry, that
someone else will complain about.
> 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?
It was not really intended that you got those features in the first place.
More information about the amber-spec-experts
mailing list