Pattern Matching for instanceof (Preview 2)

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

> On 18 Feb 2020, at 15:04, forax at wrote:
> Hi Gavin,
> ----- Mail original -----
>> De: "Gavin Bierman" <gavin.bierman at>
>> À: "Remi Forax" <forax at>
>> Cc: "jan lahoda" <jan.lahoda at>, "amber-spec-experts" <amber-spec-experts at>
>> 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. 


More information about the amber-spec-experts mailing list