From 4e2992488d27a2a4f2b75f458a8a69e20aba441c Mon Sep 17 00:00:00 2001 From: Erik Ernst Date: Thu, 7 May 2026 18:01:08 +0200 Subject: [PATCH 1/3] Add introductory section plus specification of constructor names for abbreviated constructor declarations --- .../feature-specification.md | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/accepted/future-releases/primary-constructors/feature-specification.md b/accepted/future-releases/primary-constructors/feature-specification.md index 5e76ccebc3..cc282f9d87 100644 --- a/accepted/future-releases/primary-constructors/feature-specification.md +++ b/accepted/future-releases/primary-constructors/feature-specification.md @@ -438,6 +438,48 @@ could add another non-redirecting generative constructor which could initialize `w` with some other value, in which case we must also initialize `w` as shown. +### Abbreviations of in-body constructor declarations + +This feature includes an element which is technically independent of +primary constructors, but it is related in that it also allows for +declaring constructors more concisely, except that this is concerned with +regular (non-primary) constructors in the body of the enclosing +declaration. + +Here are some examples of today's constructor declaration syntax: + +```dart +class MyClass { + const MyClass(); + MyClass.name(); + MyClass.redir(): this.name(); + factory MyClass.fact() => .new(); + factory MyClass.redirFact() = MyClass; +} +``` + +With the new, abbreviated syntax the following declarations can be used to +declare constructors that work exactly the same: + +```dart +class MyClass { + const new (); + new name(); + new redir(): this.name(); + factory fact() => .new(); + factory redirFact() = MyClass; +} +``` + +In short, the class name and the period are replaced my the keyword `new` +(in a generative constructor) or `factory` (in a factory constructor). + +*As a matter of formatting, note that the keyword `new` and the formal +parameter list `()` are separated by a space to indicate that `new` is a +keyword rather than an identifer. Similarly for `factory`. This is in line +with the convention to write `if (cond) ...` and `while (cond) ...` rather +than `if(cond) ...` and `while(cond) ...`.* + ## Specification ### Syntax @@ -588,6 +630,23 @@ constructors as well. ('=' )?; ``` +The grammar rules above introduce abbreviated constructor declarations +which are derived using the rule `` and +``. Those declarations have the same meaning as the +constructor declarations available in pre-feature Dart and are subject to +the same rules and static analysis and semantics, except for how the name +of the constructor is determined: + +A constructor declaration containing tokens `new id` derived from +`` *(i.e., `id` is an `` or absent)* in a +membered, type introducing declaration named `C` has the name `C.id` when +`id` is present, and `C` when it is absent. + +Similarly, a constructor declaration containing tokens `factory id` derived +from `` in a membered, type introducing declaration +named `C` has the name `C.id` when `id` is present, and `C` when it is +absent. + A _primary constructor_ declaration consists of a `` in the declaration header plus optionally a member declaration in the body that starts with a ``. From 36f9dd640525a60a82082f475997e230ea350772 Mon Sep 17 00:00:00 2001 From: Erik Ernst Date: Thu, 7 May 2026 18:12:16 +0200 Subject: [PATCH 2/3] Small improvements --- .../feature-specification.md | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/accepted/future-releases/primary-constructors/feature-specification.md b/accepted/future-releases/primary-constructors/feature-specification.md index cc282f9d87..957bfcf7b6 100644 --- a/accepted/future-releases/primary-constructors/feature-specification.md +++ b/accepted/future-releases/primary-constructors/feature-specification.md @@ -440,11 +440,10 @@ initialize `w` with some other value, in which case we must also initialize ### Abbreviations of in-body constructor declarations -This feature includes an element which is technically independent of -primary constructors, but it is related in that it also allows for -declaring constructors more concisely, except that this is concerned with -regular (non-primary) constructors in the body of the enclosing -declaration. +This feature includes a subfeature which is technically independent of +primary constructors, but it is related in that it also allows for more +concise constructor declarations. This subfeature is concerned with regular +(non-primary) constructors in the body of the enclosing declaration. Here are some examples of today's constructor declaration syntax: @@ -454,7 +453,7 @@ class MyClass { MyClass.name(); MyClass.redir(): this.name(); factory MyClass.fact() => .new(); - factory MyClass.redirFact() = MyClass; + const factory MyClass.redirFact() = MyClass; } ``` @@ -467,18 +466,16 @@ class MyClass { new name(); new redir(): this.name(); factory fact() => .new(); - factory redirFact() = MyClass; + const factory redirFact() = MyClass; } ``` In short, the class name and the period are replaced my the keyword `new` -(in a generative constructor) or `factory` (in a factory constructor). +(in a generative constructor) or simply removed (in a factory constructor). *As a matter of formatting, note that the keyword `new` and the formal parameter list `()` are separated by a space to indicate that `new` is a -keyword rather than an identifer. Similarly for `factory`. This is in line -with the convention to write `if (cond) ...` and `while (cond) ...` rather -than `if(cond) ...` and `while(cond) ...`.* +keyword rather than an identifer. Similarly for `factory`.* ## Specification From cb5443ff1fe8bfe104332c2b251380b204a50b1e Mon Sep 17 00:00:00 2001 From: Erik Ernst Date: Wed, 13 May 2026 18:13:50 +0200 Subject: [PATCH 3/3] Remove the keyword space --- .../primary-constructors/feature-specification.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/accepted/future-releases/primary-constructors/feature-specification.md b/accepted/future-releases/primary-constructors/feature-specification.md index 957bfcf7b6..d4a942d0e4 100644 --- a/accepted/future-releases/primary-constructors/feature-specification.md +++ b/accepted/future-releases/primary-constructors/feature-specification.md @@ -462,7 +462,7 @@ declare constructors that work exactly the same: ```dart class MyClass { - const new (); + const new(); new name(); new redir(): this.name(); factory fact() => .new(); @@ -473,10 +473,6 @@ class MyClass { In short, the class name and the period are replaced my the keyword `new` (in a generative constructor) or simply removed (in a factory constructor). -*As a matter of formatting, note that the keyword `new` and the formal -parameter list `()` are separated by a space to indicate that `new` is a -keyword rather than an identifer. Similarly for `factory`.* - ## Specification ### Syntax