-
Notifications
You must be signed in to change notification settings - Fork 214
Change more elements of augmenting type declarations to be optional? #3694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Allowing omitting values from enums is tricky, because an enum value declaration can look like a function declaration: enum Baz with Qux {
// Banana!
foo(x);
final int x;
const Baz(this.x);
}
const x = 42;
mixin Qux {
int foo(int x) => x;
} Here the TL;DR: Making the values of an enum optional makes the grammar ambiguous. We can allow an empty value list, ended by enum Baz with Qux {
; // No values yet.
// Banana!
foo(x);
final int x;
const Baz(this.x);
} Then we'll make it a compile-time error if a fully augmented enum declaration has no enum values. For extension types, I'd choose to not be too strict. If we don't introduce the representation getter until we have the fully augmented declaration, then there is no problem with adding it more than once. (And we need the fully augmented declaration to check for conflicts anyway.) That will not allow an extension type to keep the representation declaration abstract, omitting it in the initial declaration, and have the representation type and name declared by an augmentation.
|
I think this is related to #3879 |
Yes, this issue is basically a special case of the topic in #3879. |
Sounds fine to me.
Maybe, I don't see an issue with it at least, although I am not sure of the use case.
Agreed
I would allow inheriting the type possibly, but not filling it in. We don't allow augmentations of functions to fill in missing types on parameters, and this sounds the same as that.
I just wouldn't allow this, I don't see a need and the same argument regarding function augmentations applies. |
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.
Consider the following program:
This program could be considered well-formed because the augmenting type declaration (concretely,
extension type
declaration, but this kind of consideration would apply to classes, mixins, etc. as well) repeats the "header" of the declaration, and it is then able to add new members or augment existing ones by adding declarations to the body.However, we could also consider the syntax
(int i)
in the header of the declarations ofET
to be a syntactic abbreviation of an instance variable declaration and a constructor declaration, following the nature of the primary constructor proposal. If we adopt this perspective then we would have a conflict because the original declaration ofET
declares a final instance variable namedi
, and so does the augmenting declaration ofET
, and that's a conflict even in the case where the two declarations are identical. Similarly for the constructor.We could solve this problem by adding support for
(augment int i)
, which would mean that the declaration ofi
in the augmenting declaration ofET
is an augmentation of the one in the initial declaration ofET
(and it has no effect).Alternatively, we could allow a declaration like
augment extension type ET implements int {}
which would correspond to a declaration ofET
that does not contribute that final instance variable and constructor declaration, so there's no conflict.Another example is augmenting
enum
declarations. We might want to allow those to omit the declarations of values, which would be useful in the case where the augmenting declaration is intended to provide additional instance or static members, but no new values are added, and no augmentation is performed on any of the existing members:This is currently a syntax error because the list of
<enumEntry>
declarations is required. We could specify a new syntax for anaugment enum
declaration such that this list is optional.The text was updated successfully, but these errors were encountered: