Dart - generate constructor with named params#6751
Dart - generate constructor with named params#6751wing328 merged 2 commits intoOpenAPITools:masterfrom
Conversation
| {{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{ | ||
| {{/allowableValues}} | ||
| {{/vars}} | ||
| {{classname}}(); |
There was a problem hiding this comment.
@agilob I'm no expert in Dart but shall we keep the default constructor (no argument) for backward compatibility?
(upcoming 5.0.0 allows breaking changes without fallbacks but it's still worth considering backward compatibility)
There was a problem hiding this comment.
Sure, happy to explain this feature. Dart has flexible syntax on method params, default values, and mandatory params.
Method/constructor params can be mandatory (as not named) or named (optional) or named AND mandatory. This change introduces constructors that have named optional parameters. Such parameters have input params inside {} brackets. Since all fields in these classes are public anyway (dart discourages use of setters and getters "just because" like in java), so we have this syntax, of all valid constructors.
Constructors with all optional params are backwards compatible, it's really just programmers' preference

This MR introduces optional named params in constructors, while cascade operators is already supported, it's fully backwards compatible.
https://dart.dev/guides/language/language-tour#optional-parameters
https://dart.dev/guides/language/language-tour#cascade-notation-
(upcoming 5.0.0 allows breaking changes without fallbacks but it's still worth considering backward compatibility)
I am myself a guardian of backwards compatibility ;)
This MR 6729 breaks backwards compatibility when creating enums (which now are just string without any validation), so I found it necessary.
There was a problem hiding this comment.
I am myself a guardian of backwards compatibility ;)
👍
Thanks for the explanation. The change looks good to me.
If no further feedback or question, I'll merge it over the weekend.
not for properties with default values for example: generates class Foo {
String bar = "some default value";
Foo({
this.bar,
});
// ...
}
print(Foo().bar); // prints null, used to print 'some default value' |
|
@Mike278 this is fixed now on master |
PR checklist
./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/config/java*. For Windows users, please run the script in Git BASH.master@ircecho (2017/07) @swipesight (2018/09) @jaumard (2018/09) @athornz (2019/12) @amondnet (2019/12)