Skip to content

Commit 0f33c2f

Browse files
authored
Add rules to allow omission of certain default values (#4393)
This PR changes the rules about augmentation of constructors such that an incomplete factory is able to defer the decision about implementing the constructor by a redirection or by a body. This is done by making the rules about default values of formal parameters more flexible: The default value can be specified in multiple locations as long as there is at most one of them.
1 parent c95dd33 commit 0f33c2f

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

working/augmentations/feature-specification.md

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Author: rnystrom@google.com, jakemac@google.com, lrn@google.com
44

5-
Version: 1.37 (see [Changelog](#Changelog) at end)
5+
Version: 1.38 (see [Changelog](#Changelog) at end)
66

77
Experiment flag: augmentations
88

@@ -453,7 +453,7 @@ scope. *We could say that it attaches itself to the existing name.*
453453

454454
Augmentations aren't allowed to *replace* code, so they mostly add entirely new
455455
declarations to the surrounding type. However, function and constructor
456-
augmentations can fill in a body for an augmented declaration that is lacks one.
456+
augmentations can fill in a body for an augmented declaration that lacks one.
457457

458458
More precisely, a function or constructor declaration (introductory or
459459
augmenting) is *incomplete* if all of:
@@ -771,13 +771,45 @@ augment class Person {
771771
}
772772
```
773773

774+
A top-level function, static method, or instance method may be augmented to
775+
provide default values for optional parameters:
776+
777+
```dart
778+
class C {
779+
void m1([int i]);
780+
void m2({String name});
781+
void m3({String otherName = "Smith"}); // OK, too.
782+
}
783+
784+
augment class C {
785+
augment m1([i = 1]) {}
786+
augment m2({name = "John"}) {}
787+
augment m3({otherName}) {}
788+
}
789+
```
790+
791+
An optional formal parameter has the default value _d_ if exactly one
792+
declaration of that formal parameter in the augmentation chain specifies a
793+
default value, and it is _d_. An optional formal parameter does not have an
794+
explicitly specified default value if none of its declarations in the
795+
augmentation chain specifies a default value. The default value is
796+
introduced implicitly with the value null in the case where the parameter
797+
has a nullable declared type, and no default values for that parameter are
798+
specified in the augmentation chain.
799+
774800
It's a **compile-time** error if:
775801

776802
* The signature of the augmenting function does not [match][signature
777803
matching] the signature of the augmented function.
778804

779-
* The augmenting function specifies any default values. *Default values are
780-
defined solely by the introductory function.*
805+
* More than one declaration in the augmentation chain specifies a default
806+
value for the same optional parameter. This is an error even in the
807+
case where all of them are identical. *Default values are defined by
808+
the introductory function or an augmentation, but at most once.*
809+
810+
* No declaration in the augmentation chain specifies a default value for
811+
an optional parameter whose declared type is potentially non-nullable,
812+
and the declared function is not abstract.
781813

782814
* A function is not complete after all augmentations are applied, unless it's
783815
an instance member and the surrounding class is abstract. *Every function
@@ -848,16 +880,30 @@ Augmenting constructors works similar to augmenting a function, with some extra
848880
rules to handle features unique to constructors like redirections and
849881
initializer lists.
850882

883+
It is **not** a compile-time error for an incomplete factory constructor to
884+
omit default values. *That is, they are treated similarly to abstract
885+
instance methods in this respect. This allows the augmenting declaration to
886+
implement the constructor by adding a redirection or a body.*
887+
851888
It's a **compile-time error** if:
852889

853890
* The signature of the augmenting function does not [match][signature
854891
matching] the signature of the augmented function.
855892

856-
* The augmenting constructor parameters specify any default values.
857-
*Default values are defined solely by the introductory constructor.*
893+
* More than one declaration in the augmentation chain specifies a default
894+
value for the same optional parameter. This is an error even in the
895+
case where all of them are identical. *Default values are defined by
896+
the introductory declaration or an augmentation, but at most once.*
897+
898+
* The augmentation chain has exactly one specification of a default value
899+
for an optional parameter, and the constructor is a redirecting factory.
900+
901+
* No declaration in the augmentation chain specifies a default value for
902+
an optional parameter whose declared type is potentially non-nullable,
903+
and the constructor is not a redirecting factory.
858904

859905
* The introductory constructor is `const` and the augmenting constructor
860-
is not or vice versa. *An augmentation can't change whether or not a
906+
is not, or vice versa. *An augmentation can't change whether or not a
861907
constructor is const because that affects whether users are allowed to use
862908
the constructor in a const context.*
863909

@@ -1128,6 +1174,11 @@ and assume the third point is always true.
11281174

11291175
## Changelog
11301176

1177+
### 1.38
1178+
1179+
* Generalize the treatment of default values of optional parameters
1180+
(addressing issue #4172).
1181+
11311182
### 1.37
11321183

11331184
* Rename to "augmentations" (from "augmentation libraries") and define the

0 commit comments

Comments
 (0)