IssueRaiser

Static nested classes

Allow declaring a class in the body of another class. ```dart class Person { class Occupation { String jobTitle; Occupation(this.jobTitle); String named(String name) => moniker(Person(name, this)); } String name; Occupation occupation; Person(this.name, this.occupation); static String moniker(Person p) => '${p.name} The ${p.occupation.jobTitle}'; } Person p = Person("Rosie", Person.Occupation("Riveter")); ``` I'd expect that `Occupation` is not an instance variable of `Person`, if that would even be a sensible notion in Dart, instead the outer class acts as a namespace containing the inner class. The inner class cannot be accessed from a `Person` instance. The inner class cannot capture instance variables from the outer class. It can howewer access static methods and fields. Nested classes could be used for: * Indicating a relationship between classes / encapsulating classes which only serve to complement another (e.g. Flutter `StatefulWidget`s and their respective `State`s) * Creating arbitrary namespaces like `export _ as _` but without a library declaration file * Encapsulating many related methods under a namespace

Posted by dz4k on Apr 29, 2019, 7:34 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

github (Other)

Static nested classes

dz4kApr 29, 2019, 7:34 PM

Allow declaring a class in the body of another class. ```dart class Person { class Occupation { String jobTitle; Occupation(this.jobTitle); String named(String name) => moniker(Person(name, this)); } String name; Occupation occupation; Person(this.name, this.occupation); static String moniker(Person p) => '${p.name} The ${p.occupation.jobTitle}'; } Person p = Person("Rosie", Person.Occupation("Riveter")); ``` I'd expect that `Occupation` is not an instance variable of `Person`, if that would even be a sensible notion in Dart, instead the outer class acts as a namespace containing the inner class. The inner class cannot be accessed from a `Person` instance. The inner class cannot capture instance variables from the outer class. It can howewer access static methods and fields. Nested classes could be used for: * Indicating a relationship between classes / encapsulating classes which only serve to complement another (e.g. Flutter `StatefulWidget`s and their respective `State`s) * Creating arbitrary namespaces like `export _ as _` but without a library declaration file * Encapsulating many related methods under a namespace

Comments (0)

No comments yet. Start the discussion.

Comment on this issue