Clarifying record reflective support

forax at univ-mlv.fr forax at univ-mlv.fr
Wed Dec 4 01:05:38 UTC 2019


----- Mail original -----
> De: "Maurizio Cimadamore" <maurizio.cimadamore at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Chris Hegarty" <chris.hegarty at oracle.com>, "amber-spec-experts" <amber-spec-experts at openjdk.java.net>, "joe darcy"
> <joe.darcy at oracle.com>
> Envoyé: Mercredi 4 Décembre 2019 01:26:11
> Objet: Re: Clarifying record reflective support

> On 04/12/2019 00:17, forax at univ-mlv.fr wrote:
>> I don't follow you, conceptually a pattern matching is an optional of
>> tuples (i don't want to go more deep than that given that the real
>> implementation is still in flux), you can construct an optional if
>> getReturnComponents() returns null or not.
> 
> Right - but what happens when you do:
> 
> Class<?> x = ...
> if (x instanceof Class.recordComponents(var components)) {
>    ...
> }

I believe you mean something like 'matches' not 'instanceof'.

> 
> If the method Class::getRecordComponents always returns something (e.g.
> an empty array) and we claim that a deconstructor here is the dual case
> of that, that might suggest that the pattern above always matches (in
> some cases with 'components' bound to an empty array). My guts is that
> people writing the above code will want it NOT to match if 'x' is not a
> record class; but if you go there, then you have what seems like an
> asymmetry: I can ask String.class.getRecordComponents (and get an empty
> array) but I can pattern-match String.class, because the extractor also
> does the 'isRecord' trick, and fail to match if that returns false.
> 
> Not saying it's not doable... but feels more awkward for some reason.

Calling an arbitrary methods inside an instanceof/switch is pure evil for me,
i've been roughly bitten several times by implementations of the method unapply() in Scala doing side-effects and i see no point to introduce such shenanigans in Java.

For 'matches', i suppose it should be based on Optional and/or null, being based on both is a kind of cool, like the enhanced for can be used on array or on Iterable, but is it enough to pass the bar to be included in Java ?
If it works on something like an Optional, you may have to use filter:

  if (x matches Class.recordComponents(var components).filter(Class::isRecord)) {

  }


> 
> Maurizio

Rémi


More information about the amber-spec-experts mailing list