Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Author: Erik Ernst

Status: Accepted

Version: 1.12
Version: 1.13

Experiment flag: declaring-constructors

Expand Down Expand Up @@ -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
Copy link
Member

@lrhn lrhn Dec 3, 2025

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:

An extension type declaration must have a primary constructor and its one parameter is always declaring,
the representation variable cannot be declared
using a normal instance variable declaration.

)

instance variable declaration):

```dart
// Using a primary constructor.
Expand Down Expand Up @@ -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
initializer list of the body part of a primary constructor.
Copy link
Member

Choose a reason for hiding this comment

The 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!
Could we consider calling it something else, like "the this part" or "the primary constructor body section" (probably not).


*This includes expressions like `p++` where the assignment is implicit.*

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`:
Expand Down Expand Up @@ -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
Copy link
Member

@lrhn lrhn Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If the parameter is var p or final p, where its declared type is inherited, inferred from default value or defaults to Object?, then this text doesn't cover that situation.

So consider:

Otherwise it's a declaring parameter.
Let p be the declared name of the parameter and
T its declared type (potentially inferred).

If D is not an extension type declaration,
the declaring parameter's name, type and preceding var, final and or covariant is replaced in L2 by
the initializing formal this.p.
Then a semantic instance variable declaration
is added to D2 with name p and type T.
The variable is final if the declaring parameter
of L had a final modifier, otherwise it's not.
The variable iscovariant if the declaring parameter
had a `covariant modifier, otherwise it's not.

If D is an extension type declaration,
it's a compile-time error if the declaring parameter of L
has a var modifier. It's always a compile-time error if
a declaring parameter has a covariant modifier and no var,
so a valid extension type primary constructor parameter can never have a covariant modifier.

(Is it a compile-time error if it has no type clause, even if it has a default value to infer it from?)
Otherwise the parameter in L2 is the same as in L1.

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
Copy link
Member

@lrhn lrhn Dec 3, 2025

Choose a reason for hiding this comment

The 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 required modifier".

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the "and D is not an extension type declaration to the prior sentence. The entire variable declaration is not introduced if it's an extension type declaration, not just the final.

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.
Expand Down Expand Up @@ -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
Expand Down