-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dart - generate constructor with named params #6751
Dart - generate constructor with named params #6751
Conversation
@@ -6,7 +6,12 @@ class {{classname}} { | |||
{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{ | |||
{{/allowableValues}} | |||
{{/vars}} | |||
{{classname}}(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.sh
to 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)