Skip to content

Commit 82ef8eb

Browse files
authored
update extension type augmentations to be more strict (#4003)
Closes #3694. I went for the simplest answer here, since I don't see a motivating use case for anything more complex. - The "original" extension type declaration _must_ include the representation type. - Augmentations of extension types _must not_ include the representation type. There is a TODO for updating the grammar, I think the simplest option is to make the representation type optional for both regular extension type declarations and augmenting ones, and have the error introduced at a later stage. This would have better behavior for tooling so that the file can still be parsed (ie: the formatter can still format in the face of this error). But, that is just a suggestion and I don't feel strongly if we would prefer to make the grammar more complex and have special rules for each.
1 parent 525d45d commit 82ef8eb

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

working/augmentation-libraries/feature-specification.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Augmentations
22

33
4-
Version: 1.29 (see [Changelog](#Changelog) at end)
4+
Version: 1.30 (see [Changelog](#Changelog) at end)
55

66
Augmentations allow spreading your implementation across multiple locations,
77
both within a single file and across multiple files. They can add new top-level
@@ -939,13 +939,29 @@ It is a compile-time error if:
939939
When augmenting an extension type declaration, the parenthesized clause where
940940
the representation type is specified is treated as a constructor that has a
941941
single positional parameter, a single initializer from the parameter to the
942-
representation field, and an empty body.
943-
944-
This means that an augmentation can add a body to an extension type's
945-
constructor, which isn't otherwise possible. *(But note that there is no
942+
representation field, and an empty body. The representation field clause must
943+
be present on the declaration which introduces the extension type, and must be
944+
omitted from all augmentations of the extension type.
945+
946+
**TODO**: Update the grammar to allow extension types to omit the parenthesized
947+
clause with the representation type (or possibly only augmentations of extension
948+
types, but it is probably better to make this a semantic error and not a
949+
syntactic one to provide a better dev experience).
950+
951+
This means that an augmentation can add a body to an extension type's implicit
952+
constructor, which isn't otherwise possible. This is done by augmenting the
953+
constructor in the body of the extension type *(But note that there is no
946954
guarantee that any instance of an extension type will have necessarily executed
947955
that body, since you can get instances of extension types through casts or other
948-
conversions that sidestep the constructor.)*
956+
conversions that sidestep the constructor.)*. For example:
957+
958+
```dart
959+
extension type A(int b) {
960+
augment A(int b) {
961+
assert(b > 0);
962+
}
963+
}
964+
```
949965

950966
*This is designed in anticipation of supporting [primary constructors][] on
951967
other types in which case the extension type syntax will then be understood by
@@ -956,6 +972,10 @@ looks and behaves much like one, and it cannot be augmented as such. It is a
956972
compile time error to have any augmenting declaration with the same name as the
957973
representation object.
958974

975+
It is a compile time error if:
976+
977+
* An extension type augmentation contains a representation field clause.
978+
959979
[primary constructors]:
960980
https://github.com/dart-lang/language/blob/main/working/2364%20-%20primary%20constructors/feature-specification.md
961981

@@ -1367,6 +1387,11 @@ to the augmentation.
13671387

13681388
## Changelog
13691389

1390+
### 1.30
1391+
1392+
* Simplify extension type augmentations, don't allow them to contain the
1393+
representation type at all.
1394+
13701395
### 1.29
13711396

13721397
* Simplify enum value augmentations, no longer allow altering the

0 commit comments

Comments
 (0)