Records feedback

Florian Weimer fw at deneb.enyo.de
Sat Nov 23 13:18:30 UTC 2019


* Brian Goetz:

>> - I find the requirement to have a public constructor undesirable,
>> although I understand the rationale. Having spent years avoiding
>> constructors, I don't want to go back to writing `new` everywhere.
>> This may well need "factory as a language feature", but such as thing
>> would surely be a good thing. Even allowing Foo.new() would be a step
>> forward.
>
> Yes, we spent a fair amount of time struggling with this.  I agree
> that “new” has the look of “old” libraries.  And we did explore
> whether it was practical to do something about factories in the
> language (including what you suggest, effectively allowing “new” as a
> name of a static factory method.)  In the end, we decided it wasn’t
> worth it.  Effective Java lists the following advantages of factories
> over constructors:
>
>  - Can have informative names / avoid subtle override tricks
>  - Can return an instance of a subtype
>  - Can return a cached instance
>  - Can take advantage of type inference for generic type parameters

On the other hand, for a record, a public constructor may not be able
to check the record invariant efficiently based on the fields alone.
If the record were constructed by other means and there is no public
constructor, such checking would not be necessary.

Ensuring deep immutability could be such an expensive check, I think.
There are likely other examples.


More information about the amber-dev mailing list