Pattern Matching for instanceof (Preview 2)

Gavin Bierman gavin.bierman at oracle.com
Tue Feb 18 14:32:12 UTC 2020


[Just circling back to this, as I added a note about the grammar to the JEP page…]

> On 6 Feb 2020, at 20:38, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> [moved to amber-spec]
> 
> ----- Mail original -----
>> De: "jan lahoda" <jan.lahoda at oracle.com>
>> À: "amber-dev" <amber-dev at openjdk.java.net>
>> Envoyé: Jeudi 6 Février 2020 21:18:52
>> Objet: Pattern Matching for instanceof (Preview 2)
> 
>> Hi,
>> 
>> Thanks to Gavin, Brian and Alex, there is a new draft JEP for Pattern
>> Matching for instanceof (Preview 2):
>> https://bugs.openjdk.java.net/browse/JDK-8235186
>> 
>> Any feedback on the JEP is more than welcome!
>> 
>> Thanks,
>>     Jan
> 
> so the difference with the previous preview is that deconstruction is added.

That is correct.

> 
> I see two questions:
> - the grammar allows to mix var and non-var for a given reference type, i think that should only permitted if the non var is a deconstruction itself ?

I don’t know what you mean here. There are two patterns, a type test pattern and a deconstruction pattern. In v2 we propose to support deconstruction patterns over record types *only*. A deconstruction pattern looks like this: Point(var a, var b), i.e. all the components are either (recursively) deconstruction patterns, or `var` <identifier>, i.e. with no type needed. I added a note to the JEP page pointing out that this is a starting point, and eventually we will support other patterns in the argument position, specifically <type> <identifier>; hopefully in this release.


> - must the identifier of a pattern argument be the same name as the corresponding record component ?
>  To be coherent with the fact that constructors requires the same names.

Absolutely not! Note this does mean that you can write confusing code:

record Point(int x, int y) { }

if (o instanceof Point(var y, var x)) { 
… // y refers to x component, y refers to x component
}

(I’ll get my coat…)

Thanks,
Gavin


More information about the amber-spec-observers mailing list