Pattern Matching for instanceof (Preview 2)

forax at univ-mlv.fr forax at univ-mlv.fr
Tue Feb 18 15:04:47 UTC 2020


Hi Gavin,

----- Mail original -----
> De: "Gavin Bierman" <gavin.bierman at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "jan lahoda" <jan.lahoda at oracle.com>, "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Mardi 18 Février 2020 15:32:12
> Objet: Re: Pattern Matching for instanceof (Preview 2)

> [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.

Currently we don't support mixing var and non var in lambda parameters.
So my question is: does this pattern Point(var x, int y) that mix a 'var' and an explicit type allowed or not ?

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

yes, that's why i ask, it leads to very confusing code, some languages don't allow this kind of code,
by example destructuring in JavaScript as a special syntax if you want to use different names.

> 
> Thanks,
> Gavin

regards,
Rémi


More information about the amber-spec-observers mailing list