[records] Is null back?

Remi Forax forax at univ-mlv.fr
Sat Mar 7 23:26:43 UTC 2020


----- Mail original -----
> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Sascha Kohlmann" <Sascha.Kohlmann at misterspex.de>, "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Samedi 7 Mars 2020 23:57:58
> Objet: Re: [records] Is null back?

> Is null back?  It never left us!
> 
> In addition to the other answers you got, I'd add:
> 
> 
>> I found it not very intuitive to use "this.value" if I don't see the instance
>> variable.
>>
>> record ConcreteId(String id) implements Identity {
>>      public ConcreteId {
>>          this.id = requireNonNull(id);
>>      }
>> }
>>
> 
> The intention is that you don't have to say ``this` here at all, and
> probably shouldn't.   The compact constructor commits its arguments to
> the same-named fields at the end of the ctor, if they are DU at the end
> of the ctor.  So the better way to do this would be:
> 
> record ConcreteId(String id) implements Identity {
>     public ConcreteId {
>         id = requireNonNull(id);
>     }
> }
> 
> as constructor arguments are mutable.  Think of the body of the compact ctor as
> a transform on its parameters, which are then committed to the state of the
> record.

or even
record ConcreteId(String id) implements Identity {
  public ConcreteId {
    requireNonNull(id);
  }
}

as a trivia, in both case, the generated bytecode is bigger than the initial code of Sascha.

Rémi


More information about the amber-dev mailing list