javac rejects canonical constructor when formal parameter name doesn't match the record component name

Tagir Valeev amaembo at gmail.com
Tue Dec 24 10:50:04 UTC 2019


Hello!

javac 14-ea+28-1366 rejects the following code:

record R(int x) {
  public R(int _x) { this.x = _x; }
}

Test.java:2: error: invalid canonical constructor in record R
  public R(int _x) { this.x = _x; }
         ^
  (invalid parameter names in canonical constructor)
Note: Test.java uses preview language features.
Note: Recompile with -Xlint:preview for details.

However, the spec for the records [1] doesn't prohibit such a
renaming. It says only that "The types of the formal parameters in the
formal parameter list of the canonical constructor must be identical
to the declared type of the corresponding record component." The
formal parameter names are only mentioned in "derived constructor
signature" definition. However, this definition doesn't prohibit
anything, to my understanding. So either compiler should be updated to
allow such a code or spec should be updated to explicitly prohibit it.

With best regards,
Tagir Valeev.

[1] http://cr.openjdk.java.net/~gbierman/jep359/jep359-20191125/specs/records-jls.html#jls-8.10.4


More information about the amber-dev mailing list