[records] Is null back?

Brian Goetz brian.goetz at oracle.com
Sat Mar 7 22:57:58 UTC 2020


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.




More information about the amber-dev mailing list