RFR: JDK-8236598: javac rejects record component with 'final' modifier

John Rose john.r.rose at oracle.com
Tue Jan 7 03:50:37 UTC 2020


On Jan 6, 2020, at 6:51 PM, Tagir Valeev <amaembo at gmail.com> wrote:
> 
> Btw how about finality of compact constructor parameters? I assume that if I declare the records component final and declare a compact constructor, its formal parameters become final as well. Or not? If we disable 'final' on records components, this means that compact constructor formal parameters are never final?

This would be an adjustment tending to make compact constructor
parameters final, which is an anti-pattern.  Constructor parameters
are often non-final to allow the class author to perform argument
projections, such as clipping or defensive copying.  So final compact
constructor arguments needlessly narrow the sweet spot for records.
They require more ceremony to do a simple job, like array cloning.

Gavin’s draft recognizes these cases when it mentions “normalizations”,
and shows the example of NonNullString.  Let’s not give artificial
roadblocks to such use cases by propagating an unnecessary “final".

Put another way:  If for some very obscure reason you *need* constructor
parameters that are final, then write out the whole constructor with
full ceremony.

For lambda capture, implicit finality does the job better than an explicit
final modifier.  (I’m glad of that feature.)

— John (and that’s final)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20200106/1c14ce59/attachment.htm>


More information about the compiler-dev mailing list