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

Why doesn't dart allow to declare enums in class? #1050

Closed
gupta-shrinath opened this issue Jun 26, 2020 · 4 comments
Closed

Why doesn't dart allow to declare enums in class? #1050

gupta-shrinath opened this issue Jun 26, 2020 · 4 comments
Labels
question Further information is requested

Comments

@gupta-shrinath
Copy link

I tried to declare an enum inside the class and it gave me an error stating can't have enum inside a class. I wanted to know the reason why but I didn't find anything on the internet. Declaring enum inside a class is allowed by major languages why not dart?

@lrhn
Copy link
Member

lrhn commented Jun 28, 2020

Dart does not allow classes to be declared inside other classes. See #336.
Since enums are classes, they also cannot be declared inside other classes.

@lrhn lrhn added the question Further information is requested label Jun 28, 2020
@gupta-shrinath
Copy link
Author

Oh, I also thought that this might be the case. But why doesn't dart allow nested class? I just want to know I also understand that this might sound naive.

@lrhn
Copy link
Member

lrhn commented Aug 5, 2020

Dart does not allow nested classes mainly because it's simpler not to.

If you nest classes, there are two ways to do it:

  1. Static nested classes, which really is just a way to put the declaration into the namespace of the other class, but is otherwise the same as declaring another top-level class.
  2. Virtual classes, where subclasses inherit the class and can perhaps even override it.

The latter is pretty complicated, and not something we have found a good enough reason to prioritize yet.
The former is fairly simple, but also not very powerful, so it too hasn't been a priority.

@gupta-shrinath
Copy link
Author

I appreciate your reply:+1: . Closing the issue

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

No branches or pull requests

2 participants