Draft JLS spec for records

forax at univ-mlv.fr forax at univ-mlv.fr
Thu Sep 5 21:45:21 UTC 2019

----- Mail original -----
> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "John Rose" <john.r.rose at oracle.com>, "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Jeudi 5 Septembre 2019 22:55:13
> Objet: Re: Draft JLS spec for records

>> We are circulating back to my first question, why the constructor has
>> to be public.
> Think about it some more.  Suppose the default was that the constructor
> was private.  Now, without specifying a constructor, no record could be
> instantiated at all, without the user declaring a constructor!  That
> would be terrible.  Part of the point here is that the require acquires
> a standard, sensible external API _without_ the user having to declare
> these elements.  Users's get to say `record Foo(String s)`, and get
> everything they need to use the record.
> If Java had factories, then we could quite sensibly decide the mandated
> API is a private ctor and a public factory.  But it doesn't, so we can't.
> OK, I suppose the next thing you are going to say is "Sigh, I guess the
> default (implicit) constructor has to be public, but if I _explicitly_
> provide a constructor, I should be able to 'remove' it from the API."

It's not what i'm saying.


> In fact, one by one, we've been
> _removing_ gratuitous differences between records and classes, and each
> time we did, the design seems to have gotten better.

I fully agree, you should have exactly the same rules as with the classes,
so the canonical constructor should follow the same rule as the default constructor.

Also why having the constructor public helps to make the record instantiatable, a constructor to be accessible by reflection requires that both the class and the constructor to be public. So following your point, does it means that all record have to be public too ?


More information about the amber-spec-experts mailing list