diff --git a/accepted/future-releases/primary-constructors/feature-specification.md b/accepted/future-releases/primary-constructors/feature-specification.md index 5e76ccebc3..d4a942d0e4 100644 --- a/accepted/future-releases/primary-constructors/feature-specification.md +++ b/accepted/future-releases/primary-constructors/feature-specification.md @@ -438,6 +438,41 @@ 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 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: + +```dart +class MyClass { + const MyClass(); + MyClass.name(); + MyClass.redir(): this.name(); + factory MyClass.fact() => .new(); + const 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(); + const factory redirFact() = 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). + ## Specification ### Syntax @@ -588,6 +623,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 ``.