-
Notifications
You must be signed in to change notification settings - Fork 200
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
Feature request: support for nested classes. #1049
Comments
Removed Type-Defect label. |
The idea of nested classes is very familiar to us (see. e.g., Beta, Newspeak). To avoid any misunderstandings, I should make it clear that we would NOT do this the way it was done in the Java programming language. Whether this will happen in Dart is an open question. Set owner to @gbracha. |
This comment was originally written by astimo...@gmail.com I would be very happy if inner classes were only a means of taking advantage of lexical scoping for the organization of classes by name. class Foo Foo() class Bar class _Baz new Foo.Bar(); // acceptable By "only a means of taking advantage of lexical scope" I mean to exclude the java behaviour of creating a runtime reference to the outer class unless the inner class is declared static. For that case the program can explicitly provide the outer class to the inner class. Thus the following will not work: class Foo Foo() void hello() class Bar() But this will work: class Foo Foo() class Bar Bar(Foo foo) void speak() |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
This comment was originally written by PortalProgra...@gmail.com Any updates on this issue? |
Not really. |
It quite often the case that an enum has a very strong relation to a class - because of this missing feature I can't write "inner-enums" - annoying. |
You can make the enum library-private. Not the same of course, but quite close. |
Thanks - I know. But this: class MyCoolClass {
enum Props { HOT, NEW, COOL }
...
}
...
switch(props) {
case MyCoolClass.Props.HOT:
break;
...
} is what I want. I know - in Java its a bit problematic if the inner class is not static - C++ does it right. Dart should have a similar thing. |
I got it that you want that. The question is if it's worth to complicate the language for that. |
I think it's just for the communications/maintainability scalability of a systems engineering language. |
I would agree. One thing I would say as well, is that I think very rarely should dart files define multiple classes. Usually when I do see it I don't disagree with the structuring of the code, but I do find it hard to use the resulting api.
There's no easy autocomplete for "what does the something package expose again?" Its also hard when reusing this code elsewhere, to figure out which import brings in which classes. You could do prefixed imports, but now you've either got the tedious I think it would be a nice improvement if we only ever defined one class in a dart file, and relied on inner classes where they are so related that they deserve to be in the same file.
You may also not be surprised to hear that I think this is less of an issue when the classes are named Obviously there's a conflict here with the syntax, as Overall my opinion is that it would be a small but appreciable win for dart. |
@gbracha In what way would you do it differently in Dart? |
(PS: Gilad is not in the Dart team now, he went on to pursue new adventures elsewhere.) That said, I think this is the right place to gather arguments for or against adding class nesting (static or otherwise) to Dart, but also that there is relatively modest support for the idea at this point. |
@eernstg Do you have any idea what Gilad meant about doing it differently than Java? |
@eernstg Maybe like Kotlin - where an inner class is always static (Doesn't have a reference to it's outer class) except it is marked with the "inner" keyword |
Gilad's Newspeak includes nested classes, and they are related to the general nesting in BETA and gbeta (of patterns, which is a general concept that includes classes and a lot of other things in one language mechanism), and those languages all make nested classes members in a way that resembles methods (in particular, a nested class is subject to late binding). That's a rather powerful concept, aka virtual classes or virtual patterns, and — who knows — that might have been on his mind when he said ''we would NOT do this [like in Java]". On the other hand, static inner classes are even less powerful than Java inner classes, they provide nothing else than scoping (that is, certain things will have shorter names in some contexts). So I doubt it was that. In any case, virtual classes in Dart would be such a huge generalization (of many things, including the type system), so we won't get that in any reasonable amount of time. ;-) |
Yea I was trying to build a JSON to object mapping library but got stuck in this issue. |
I want Enums as sub-types of classes for more code clarity. |
In java I always enclose the http request like this. |
I find a way to resolve that. |
Without it, there is no way for nesting necessary listeners inside the appropriate class and nesting Builder class in the builder design pattern. Any updates? |
I want to use builder design pattern.Nested classes is necessary. |
Since everybody knows about it, I don't want to talk any more! |
Come on, it's been 8 years, goddamn |
@egorikem wrote:
I can understand that this is a serious source of impatience. However, this feature never made it to the top of the list. It is my impression that the main reason is that it causes Dart software to become more complex, especially syntactically (because it could push Dart software in the direction of having a lot of huge, nested In particular, it would be convenient that the names of certain entities can be abbreviated (e.g., calling a static method named Access to an emulated nested class would be different (for clients using them as type annotations, or creating new instances of them). But having rewritten a given nested class to a top-level class, it is certainly possible to use that top-level class as a type annotation, or to create instances of it (passing a reference to the emulated enclosing object in a constructor), and I don't see how this would not be a complete and faithful emulation. @mpowloka wrote:
Maybe you could give an example, and then we could explore approaches where the nesting is emulated and see how inconvenient it is? |
At least would be nice to have enum inside classes. Is very intuitive to have enums related in the same place. |
I don't understand why Java really should have context here. We can talk about nested classes inside every other language (React, Python, TS, basically most of them). And talking aside our subjective opinions above - there is no issue at all using them. For all other cases. |
@eernstg , Maybe we can choose democratic way. Aside of your opinions we can create poll, where all contributors will vote, based on their experience. And finally resolve this question? |
We can't promise a specific procedure, but an issue like this one with a lot of 👍's is taken seriously. At the same time, language changes do take time (e.g., dart-lang/sdk#22 is the big thing we're doing right now). |
this is the exact problem we faced as android developers back when i was using kotlin and android-studio instead of flutter. at first android team was working hard but then they got lazy and stuff which lead them to hack the problems instead of solving them. which lead to the creation of abominations like Android Support library which was a total hack. I guess i just have a fear of the same thing happening to flutter |
Any progress? |
Everybody is still fully occupied working on non-nullable types (implementing it, porting the standard libraries, and lots of other stuff). |
I just want to add another case where nested classes are useful. Constants that are subdivided into categories. For example: int statusCode = HttpStatusCodes.ClientError.forbidden; |
Will this ever happen? I have a case where I have a TranslationLocalizations class and I would like to group my translation string inside this class by categories. Inner classes would help with this a lot
|
Dart is already in |
If I'm not too late to the party, I have an approach(workaround) for inner/nested class: class AppConstants {
static const _Style Style = const _Style();
static const _URL Url = const _URL();
}
class _Style {
const _Style();
final EdgeInsetsGeometry buttonPadding = const EdgeInsets.all(10.0);
}
class _URL {
const _URL();
final String game = 'https://example.com';
}
main() {
var myStyleToUse = AppConstants.Style.buttonPadding;
} I would really like the know the downsides of this though, if anyone can figure out. 🍻 |
I like your idea @arnold-parge. There're 2 downsides I can see at the moment:
Happy coding. 🍻 |
@leafpetersen @munificent @lrhn – this should be a language issue? |
@kevmoo I've been refraining from moving all language issues in the SDK repository into the language repository. Obviously they belong there, but if there is no recent interest in the issue, then it might as well be left where it is. |
Can we expect this in Dart in the long term? |
There are no current priorities that make us work on nested classes. The problems it solves are just not the most urgent problems for us to solve right now. ... In the very long run everything is possible, and nothing is probable ... |
I can't see even one case where this is useful. |
#336 is the original language-repository issue for this request. Closing as duplicate of that. |
Coming from Swift background and having spent couple of weeks in Dart and Flutter, I cannot express my disappointment on not having nested types. I sincerely hope you guys will work on this sooner, rather than later. 🙏 |
Am I right that next code is not valid and related with this issue?
error:
|
@mateusfccp Here is an example on why nested types is such a needed feature; see the below swift code.
Having nested types allow us to reduce the clutter of having way too many types at global level. In the example above, all states related to Login are encapsulated inside the LoginState structure. In Dart, we will need to create LoginLoading, LoginSuccess, LoginFailure etc at the root level. For large scale projects, lack of nested types does hurt the maintainability of the codebase. |
@surajthomask Thanks for providing an example... I still don't know what advantages it does bring... Is it only the namespace-like notation? Like In Dart I usually achieve the same by using factory constructors + private classes if I don't want them to be exported. |
Is this feature on the radar? After finishing null safety? |
Unfortunately @arnold-parge 's workaround does not meet all the needs
https://dart.dev/tools/diagnostic-messages#non_constant_case_expression |
Not having nested classes is a major drawback. Any updates? |
Inner class is essential when expressing some classes are only meaningful inside another class, in tree-like structures like UI. |
I think this thread should be locked. As @lrhn said:
On top of posting in the wrong place. Some of us are lashing negative reinforcements on Flutter community maintainers and Googlers. Instead of phrasing our requests in a manner of negative reinforcements, just +1 the canonical issue (#336). If your motivation points for it is not mentioned, do mention them. |
This issue was originally filed by zhygrr...@gmail.com
It would be nice if we can introduce classes local to a given enclosing class.
This allows define class that is invisible outside of the Outer class scope.
I understand that this feature requires more investigation. As for me it has a limited power if a class is not the first class object (or a class could be a class member like fields or methods).
The text was updated successfully, but these errors were encountered: