|
2 | 2 |
|
3 | 3 |
|
4 | 4 |
|
5 |
| -Version: 1.37 (see [Changelog](#Changelog) at end) |
| 5 | +Version: 1.38 (see [Changelog](#Changelog) at end) |
6 | 6 |
|
7 | 7 | Experiment flag: augmentations
|
8 | 8 |
|
@@ -453,7 +453,7 @@ scope. *We could say that it attaches itself to the existing name.*
|
453 | 453 |
|
454 | 454 | Augmentations aren't allowed to *replace* code, so they mostly add entirely new
|
455 | 455 | 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. |
457 | 457 |
|
458 | 458 | More precisely, a function or constructor declaration (introductory or
|
459 | 459 | augmenting) is *incomplete* if all of:
|
@@ -771,13 +771,45 @@ augment class Person {
|
771 | 771 | }
|
772 | 772 | ```
|
773 | 773 |
|
| 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 | + |
774 | 800 | It's a **compile-time** error if:
|
775 | 801 |
|
776 | 802 | * The signature of the augmenting function does not [match][signature
|
777 | 803 | matching] the signature of the augmented function.
|
778 | 804 |
|
779 |
| -* The augmenting function specifies any default values. *Default values are |
780 |
| - defined solely by the introductory function.* |
| 805 | +* The augmentation chain has two or more specifications of a default value |
| 806 | + for the same optional parameter. This is an error even in the case where |
| 807 | + all of them are identical. *Default values are defined by the introductory |
| 808 | + function or an augmentation, but at most once.* |
| 809 | + |
| 810 | +* The augmentation chain has no specifications of a default value for an |
| 811 | + optional parameter whose declared type is potentially non-nullable, and |
| 812 | + the declared function is not abstract. |
781 | 813 |
|
782 | 814 | * A function is not complete after all augmentations are applied, unless it's
|
783 | 815 | 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
|
848 | 880 | rules to handle features unique to constructors like redirections and
|
849 | 881 | initializer lists.
|
850 | 882 |
|
| 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 | + |
851 | 888 | It's a **compile-time error** if:
|
852 | 889 |
|
853 | 890 | * The signature of the augmenting function does not [match][signature
|
854 | 891 | matching] the signature of the augmented function.
|
855 | 892 |
|
856 |
| -* The augmenting constructor parameters specify any default values. |
857 |
| - *Default values are defined solely by the introductory constructor.* |
| 893 | +* The augmentation chain has two or more specifications of a default value |
| 894 | + for the same optional parameter. This is an error even in the case where |
| 895 | + all of them are identical. *Default values are defined by the introductory |
| 896 | + 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 | +* The augmentation chain has no specifications of a default value for an |
| 902 | + optional parameter whose declared type is potentially non-nullable, and |
| 903 | + the constructor is not a redirecting factory. |
858 | 904 |
|
859 | 905 | * 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 |
861 | 907 | constructor is const because that affects whether users are allowed to use
|
862 | 908 | the constructor in a const context.*
|
863 | 909 |
|
@@ -1128,6 +1174,10 @@ and assume the third point is always true.
|
1128 | 1174 |
|
1129 | 1175 | ## Changelog
|
1130 | 1176 |
|
| 1177 | +### 1.38 |
| 1178 | + |
| 1179 | +* Generalize the treatment of default values of optional parameters. |
| 1180 | + |
1131 | 1181 | ### 1.37
|
1132 | 1182 |
|
1133 | 1183 | * Rename to "augmentations" (from "augmentation libraries") and define the
|
|
0 commit comments