-
Notifications
You must be signed in to change notification settings - Fork 791
Add private named parameters section #7250
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
04ad637
Add private named parameters section
conooi 11f8e44
Add more info on private named parameters section
conooi 22fdb01
Remove empty line
conooi c3fb36a
Merge branch 'main' into private-named-parameters
conooi a65a592
Add more info on private named parameters
conooi 5cd0d04
fix formatting issues
conooi e8a9b84
Refresh code excerpts
conooi f464334
Update private named parameters section and made changes to SDK const…
conooi e37bca2
Update code excerpts
conooi 8c4d41c
Fix formatting error
conooi e73eda8
Refresh code excerpts
conooi 740cde9
Reformat to fix failure
parlough ac0dd37
Ignore diagnostics that are intentionally triggered
parlough a786f6c
Merge branch 'main' into private-named-parameters
parlough 5dab6ae
Merge branch 'main' into private-named-parameters
conooi 520d40a
Merge branch 'main' into private-named-parameters
parlough 4a9b84f
Remove legacy link from private named parameters
conooi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
examples/misc/lib/language_tour/classes/point_private_new.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // #docregion initialize-private-named-after | ||
| class Point { | ||
| final double _x; | ||
| Point({required this._x}); | ||
| } | ||
| // #enddocregion initialize-private-named-after | ||
|
|
||
| // #docregion initialize-private-named-usage | ||
| var p = Point(x: 1.0); | ||
| // #enddocregion initialize-private-named-usage |
7 changes: 7 additions & 0 deletions
7
examples/misc/lib/language_tour/classes/point_private_old.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // #docregion initialize-private-named-before | ||
| class Point { | ||
| final double _x; | ||
| Point({required double x}) : _x = x; | ||
| } | ||
|
|
||
| // #enddocregion initialize-private-named-before |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -265,7 +265,10 @@ For more discussion, watch this Decoding Flutter video on tear-offs. | |||||||||||||||||||
|
|
||||||||||||||||||||
| ## Instance variable initialization | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Dart can initialize variables in three ways. | ||||||||||||||||||||
| Dart provides several ways to initialize instance variables. | ||||||||||||||||||||
| You can assign values in the declaration, | ||||||||||||||||||||
| use initializing formal parameters, | ||||||||||||||||||||
| or use an initializer list. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ### Initialize instance variables in the declaration | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -326,24 +329,6 @@ class PointB { | |||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Private fields can't be used as named initializing formals. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| {% comment %} | ||||||||||||||||||||
| Don't attach the following example to a code excerpt. | ||||||||||||||||||||
| It doesn't work on purpose and will cause errors in CI. | ||||||||||||||||||||
| {% endcomment %} | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| class PointB { | ||||||||||||||||||||
| // ... | ||||||||||||||||||||
|
|
||||||||||||||||||||
| PointB.namedPrivate({required double x, required double y}) | ||||||||||||||||||||
| : _x = x, | ||||||||||||||||||||
| _y = y; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // ... | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| This also works with named variables. | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -393,6 +378,125 @@ class PointD { | |||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ### Private named parameters | ||||||||||||||||||||
|
parlough marked this conversation as resolved.
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| :::version-note | ||||||||||||||||||||
| Using private named parameters as initializing formals | ||||||||||||||||||||
| requires a [language version][] of at least 3.12. | ||||||||||||||||||||
| ::: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| In Dart, fields starting with an underscore are private to their library. | ||||||||||||||||||||
| To initialize a private field using a named parameter, | ||||||||||||||||||||
| you can write manual assignment boilerplate | ||||||||||||||||||||
| in the initializer list: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <?code-excerpt "point_private_old.dart (initialize-private-named-before)" plaster="none"?> | ||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| class Point { | ||||||||||||||||||||
| final double _x; | ||||||||||||||||||||
| Point({required double x}) : _x = x; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| You can also initialize private fields directly | ||||||||||||||||||||
| in the constructor parameter list. | ||||||||||||||||||||
| When you prefix the named parameter with `this._`, | ||||||||||||||||||||
| the compiler automatically strips the underscore for the caller, | ||||||||||||||||||||
| allowing them to use a clean, public name: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <?code-excerpt "point_private_new.dart (initialize-private-named-after)" plaster="none"?> | ||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| class Point { | ||||||||||||||||||||
| final double _x; | ||||||||||||||||||||
| Point({required this._x}); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| In both cases, | ||||||||||||||||||||
| the caller uses the public name `x` at the call site: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <?code-excerpt "point_private_new.dart (initialize-private-named-usage)" plaster="none"?> | ||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| var p = Point(x: 1.0); | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Like regular [named parameters](/language/functions#named-parameters), you can | ||||||||||||||||||||
| make private named parameters optional or required. | ||||||||||||||||||||
| You can also provide explicit default values. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| In the following example, the `_x` parameter is optional and defaults to `null`. | ||||||||||||||||||||
| The `_y` parameter is also optional but has an explicit default value of `0.0`: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <?code-excerpt "point_alt.dart (initialize-private-named)" plaster="none"?> | ||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| class PointPrivate { | ||||||||||||||||||||
| final double? _x; // Nullable field | ||||||||||||||||||||
| final double _y; // Non-nullable field | ||||||||||||||||||||
|
|
||||||||||||||||||||
| PointPrivate({this._x, this._y = 0.0}); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @override | ||||||||||||||||||||
| String toString() => 'PointPrivate($_x, $_y)'; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| void testPrivate() { | ||||||||||||||||||||
| var p = PointPrivate(x: 1.0, y: 2.0); | ||||||||||||||||||||
| print(p); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
conooi marked this conversation as resolved.
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| #### Constraints | ||||||||||||||||||||
|
|
||||||||||||||||||||
| * **No conflicts:** Neither the private name nor the generated public name | ||||||||||||||||||||
| can match any other parameter name in the same constructor. | ||||||||||||||||||||
| * **Initializing formals only:** Named parameters in Dart generally | ||||||||||||||||||||
| can't be private. This capability is an exception that only applies | ||||||||||||||||||||
| to named parameters that are initializing formals (`this._field`). | ||||||||||||||||||||
|
conooi marked this conversation as resolved.
|
||||||||||||||||||||
| You can't use private identifiers for regular named parameters. | ||||||||||||||||||||
|
Comment on lines
+452
to
+455
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The description of this constraint is slightly incomplete. The private named parameters feature applies to both initializing formals (
Suggested change
|
||||||||||||||||||||
| * **Valid public name:** The private name must map to a valid public identifier. | ||||||||||||||||||||
|
conooi marked this conversation as resolved.
|
||||||||||||||||||||
| For example, `this._` or `this._2x` are invalid | ||||||||||||||||||||
| because they don't have valid public counterparts. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #### Usage in initializer lists | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Within the constructor's initializer list, | ||||||||||||||||||||
| reference the parameter using its private name: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <?code-excerpt "point_alt.dart (initialize-private-named-assert)" plaster="none"?> | ||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| class PointPrivateAssert { | ||||||||||||||||||||
| final double _x; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| PointPrivateAssert({required this._x}) : assert(_x >= 0); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #### Interaction with super parameters | ||||||||||||||||||||
|
|
||||||||||||||||||||
| When extending a class that uses private named parameters, | ||||||||||||||||||||
| subclasses use the public name for [super parameters][]. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| In the following example, the `Tool` class defines the private field `_price`. | ||||||||||||||||||||
| Even though the field is private, | ||||||||||||||||||||
| its corresponding named parameter is public (`price` not `_price`). | ||||||||||||||||||||
| To pass the value along, | ||||||||||||||||||||
| the `Hammer` subclass uses the public `price` identifier: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <?code-excerpt "point_alt.dart (initialize-private-named-super)" plaster="none"?> | ||||||||||||||||||||
| ```dart | ||||||||||||||||||||
| class Tool { | ||||||||||||||||||||
| final int _price; | ||||||||||||||||||||
| Tool({required this._price}); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class Hammer extends Tool { | ||||||||||||||||||||
| // Forwards to the public 'price' argument | ||||||||||||||||||||
| Hammer({required super.price}); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| [super parameters]: /resources/glossary#super-parameter | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ### Use an initializer list | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Before the constructor body runs, you can initialize instance variables. | ||||||||||||||||||||
|
|
||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.