Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
None
-
None
Description
When using @TupleConstructor, it automatically provides defaults for the parameters. This is particularly useful behavior since it allows parameters which can remain as their default (user provided or null/0/false) can be left out of the constructor. In particular, a no-arg constructor corresponds to the case of all arguments left out. This is exactly the kind of constructor which Groovy needs for default named-argument processing.
Having said that, there are times when all is required is a single simple constructor having all of the classes properties. This issue allows a parameter defaults=false to be set to enable this new behavior.
For this class:
@TupleConstructor class Person { String first, last int age }
The following normal constructor is produced:
Person(String first=null, String last=null, int age=0) { this.first = first //etc... }
After Groovy's compilation phases are complete, the following constructors are visible to Java:
Person(String first, String last, int age) { //... } Person(String first, String last) { this(first, last, 0) } Person(String first) { this(first, null) } Person() { this(null) }
Adding defaults=false as a parameter to TupleConstructor means that just the first of these will be produced. If any of the properties has a default value, when this setting is made false, an error will be flagged.