Skip to content
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

"Create missing overrides" and "Create field" fixes not available for enums #55264

Closed
srawlins opened this issue Mar 21, 2024 · 4 comments
Closed
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. devexp-refactoring Issues with analysis server refactorings P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

In this code:

abstract class C {
  void M();
}
enum E implements C {
  one, two;
}

The "create missing overrides" refactoring is not offered.

In this code:

enum E {
  one, two;
}
int f(E e) {
  return e.x;
}

The "create field" refactoring is not offered.

@srawlins srawlins added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request devexp-refactoring Issues with analysis server refactorings type-enhancement A request for a change that isn't a bug labels Mar 21, 2024
@srawlins srawlins self-assigned this Mar 21, 2024
@bwilkerson
Copy link
Member

Both would be good to have.

The "create field" refactoring is not offered.

Should we also have a "create enum constant" fix?

@NBCBM
Copy link

NBCBM commented Mar 21, 2024

The "create field" refactoring is not offered. Enums in Dart cannot have instance variables or fields. They are limited to declaring constant instances only.

To resolve these issues:

For the first scenario, if you need methods associated with enum instances, you may need to refactor your code to use a class hierarchy or a different approach altogether.
For the second scenario, if you need instance variables associated with enum instances, consider using a class instead of an enum.
Enums in Dart are primarily intended for representing a fixed set of constant values, rather than containing methods or instance variables.

@bwilkerson
Copy link
Member

Enums in Dart cannot have instance variables or fields.

The enhanced-enums feature, which shipped in 2.17, makes it possible to have instance fields in an enum, though instance fields are required to be final so that the instances can be constants.

But you raise an interesting question. We'd need to decide whether "create field" added an initializer (which isn't possible in all cases), made the field nullable, or added the field as a parameter to every constructor.

@srawlins srawlins removed their assignment Mar 26, 2024
@srawlins
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. devexp-refactoring Issues with analysis server refactorings P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants