Pattern Matching for instanceof (Preview 2)

Gavin Bierman gavin.bierman at oracle.com
Tue Feb 18 16:35:03 UTC 2020



> On 18 Feb 2020, at 15:04, forax at univ-mlv.fr wrote:
> 
> 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)
> 
>> 
>>> - 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.

I appreciate this, but requiring you to use the record component name is not very friendly if you have repeated nested patterns, e.g.

if (o instanceof Line(Point(int x, int y), Point(int x, int y))) // Error two occurrences of x and y!
{ …}

The only way around that would be to have explicit renaming operators, which is pretty ugly. 

Gavin


More information about the amber-spec-observers mailing list