-
Notifications
You must be signed in to change notification settings - Fork 229
Specify that non-plain primary parameters are immutable #4575
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
9361210
8cc6147
30ebd6f
c282a5e
bba400b
1a96004
126080c
f646f9a
ef482f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ Author: Erik Ernst | |
|
|
||
| Status: Accepted | ||
|
|
||
| Version: 1.12 | ||
| Version: 1.13 | ||
|
|
||
| Experiment flag: declaring-constructors | ||
|
|
||
|
|
@@ -191,10 +191,12 @@ that the instance variable declaration which is induced by this declaring | |
| constructor parameter is `final`. | ||
|
|
||
| In the case where the declaration is an `extension type`, the modifier | ||
| `final` on the representation variable can be specified or omitted. Note | ||
| that an extension type declaration is specified to use a primary | ||
| constructor (it is not supported to declare the representation variable | ||
| using a normal instance variable declaration): | ||
| `final` on the representation variable can be specified or omitted. It is | ||
| an error to specify the modifier `var` on the representation variable. | ||
|
|
||
| An extension type declaration is specified to use a primary constructor (it | ||
| is not supported to declare the representation variable using a normal | ||
| instance variable declaration): | ||
|
|
||
| ```dart | ||
| // Using a primary constructor. | ||
|
|
@@ -745,6 +747,12 @@ main() { | |
| } | ||
| ``` | ||
|
|
||
| It is a compile-time error if an assignment to a primary parameter occurs | ||
| in the initializing expression of a non-late instance variable, or in the | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| initializer list of the body part of a primary constructor. | ||
|
Member
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. "body part" feels a little ambiguous. The "body part of a primary constructor" is not the same as "the body of the primary constructor". The body part can have a body part! It's bodies all the way down! |
||
|
|
||
| *This includes expressions like `p++` where the assignment is implicit.* | ||
|
|
||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The following errors apply to formal parameters of a primary constructor. | ||
| Let _p_ be a formal parameter of a primary constructor in a class, mixin | ||
| class, enum, or extension type declaration _D_ named `C`: | ||
|
|
@@ -840,15 +848,16 @@ positional or named parameter remains optional; if it has a default value | |
| unchanged from _L_ to _L2_ *(this is a plain, non-declaring parameter)*. | ||
| - Otherwise, a formal parameter (named or positional) of the form `var T p` | ||
| or `final T p` where `T` is a type and `p` is an identifier is replaced | ||
|
Member
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. Nit: If the parameter is So consider:
|
||
| in _L2_ by `this.p`, along with its default value, if any. Next, a | ||
| semantic instance variable declaration corresponding to the syntax `T p;` | ||
| or `final T p;` is added to _D2_. It includes the modifier `final` if the | ||
| parameter in _L_ has the modifier `final` and _D_ is not an `extension | ||
| type` decaration; if _D_ is an `extension type` declaration then the name | ||
| of `p` specifies the name of the representation variable. In all cases, if | ||
| `p` has the modifier `covariant` then this modifier is removed from the | ||
| parameter in _L2_, and it is added to the instance variable declaration | ||
| named `p`. | ||
| in _L2_ by `this.p`, along with its default value, if any. If the | ||
|
Member
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. Also doesn't need to mention default value. If it wants to be complete, then it should also mention "any |
||
| parameter has the modifier `var` and _D_ is an extension type declaration | ||
| then a compile-time error occurs. Otherwise, a semantic instance variable | ||
| declaration corresponding to the syntax `T p;` or `final T p;` is added | ||
| to _D2_. It includes the modifier `final` if the parameter in _L_ has the | ||
| modifier `final` and _D_ is not an `extension type` decaration; if _D_ is | ||
|
Member
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. Move the "and D is not an |
||
| an `extension type` declaration then the name of `p` specifies the name | ||
| of the representation variable. In all cases, if `p` has the modifier | ||
| `covariant` then this modifier is removed from the parameter in _L2_, and | ||
| it is added to the instance variable declaration named `p`. | ||
|
|
||
| If there is an initializer list following the formal parameter list _L_ | ||
| then _k2_ has an initializer list with the same elements in the same order. | ||
|
|
@@ -899,6 +908,11 @@ of declaration, and the constructor might be non-const). | |
|
|
||
| ### Changelog | ||
|
|
||
| 1.13 - November 25, 2025 | ||
|
|
||
| * Specify that assignments to primary parameters in initialization code is | ||
| an error. | ||
|
|
||
| 1.12 - November 6, 2025 | ||
|
|
||
| * Eliminate in-body declaring constructors. Revert to the terminology where | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
(The "it's not verb to verb" phrasing feels a little like a Danglisism to me. Could it be:
)