Skip to content

How do you augment an enum without adding values? #4356

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

Open
munificent opened this issue May 6, 2025 · 2 comments
Open

How do you augment an enum without adding values? #4356

munificent opened this issue May 6, 2025 · 2 comments
Labels
augmentations Issues related to the augmentations proposal. question Further information is requested

Comments

@munificent
Copy link
Member

We allow an augmentation of an enum type to add new values and/or members ("enhanced enums"). The grammar works fine if the augmentation adds both:

enum E { a }

augment enum E {
  b;

  method() {}
}

But the enumType grammar currently requires at least one value declaration. What if you want to augment an enum to add members but no values? What should that look like?

The obvious answer is:

augment enum E {
  method() {}
}

But I'm not 100% certain that's unambiguous. If it is ambiguous, we could probably require the ;:

augment enum E {
  ;
  method() {}
}

But it would be nice to avoid that if possible. Filing this issue so we don't forget to figure this out.

@munificent munificent added question Further information is requested augmentations Issues related to the augmentations proposal. labels May 6, 2025
@lrhn
Copy link
Member

lrhn commented May 6, 2025

It is ambiguous.

augment enum Foo {
  name();
}

is both a valid enum element declaration and, if elements can be omitted, a valid abstract method declaration (which you may want to write to update the dartdoc for it).

Maybe require that an augmenting enum acknowledges the existing elements:

augment enum Foo {
  ..., 
  foo3;
}

(That ... is literal.)

If it doesn't start with ..., it doesn't declare elements.

However, that leaves us with the same problem for the original non-augmenting declaration.
If you can add elements in an augmentation, you can also want to start out without any elements in the first declaration.

I think requiring a leading ; whenever you declare any non-element members is fine.
That is already the rule, you can omit ; if there are no members after it, so it's more of a "members start here" separator than an elements list terminator.

(Consider dropping the augment keyword, there should be no real difference between the original declaration and an augmenting declaration anyway, especially if the latter declares no augmenting members.)

@munificent
Copy link
Member Author

It is ambiguous.

augment enum Foo {
  name();
}

Ah, good catch!

However, that leaves us with the same problem for the original non-augmenting declaration. If you can add elements in an augmentation, you can also want to start out without any elements in the first declaration.

Yes, definitely. I suspect that this is how most enums that have augmented values will be declared.

I think requiring a leading ; whenever you declare any non-element members is fine. That is already the rule, you can omit ; if there are no members after it, so it's more of a "members start here" separator than an elements list terminator.

SGTM.

This does raise the natural question of whether it's allowed to declare an enum with zero values that isn't later augmented by having more values added. Probably not useful but I guess it's a thing we could technically allow. Should we?

(Consider dropping the augment keyword, there should be no real difference between the original declaration and an augmenting declaration anyway, especially if the latter declares no augmenting members.)

I do like having the augment modifier. If a user accidentally has two top level declarations with the same name, I'd rather report an error than silently treat one of them as an augmentation which could lead to other very confusing errors if the two declarations aren't the same kind, or have colliding members or any other kind of weird overlap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
augmentations Issues related to the augmentations proposal. question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants