-
Notifications
You must be signed in to change notification settings - Fork 208
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
Static classes #2270
Comments
@nate-thegrate do enhanced enums fit your use case? void main() {
print(Shape.values);
print(Shape.polygons);
print(Shape.rectangles);
}
enum Shape {
circle(0),
triangle(3),
rect(4),
square(4);
final int sides;
const Shape(this.sides);
static List<Shape> get polygons => [triangle, square, rect];
static List<Shape> get rectangles => [
for (final polygon in polygons)
if (polygon.sides == 4) polygon
];
} The main issue is it doesn't allow for custom instances (for example You do also need to specify |
The Dart style guide actively discourages that approach.
Adding a private constructor to prevent extension is "clever", but pointless. You can still implement the interface, and extending the class has absolutely no benefit to anyone (unlike Java, Dart doesn't "inherit" static members), so it's just adding code to prevent ... well, nothing. If someone wants to extend What you are asking for here is a plain namespace. Dart For now, instead of extension Colors on Never {
// static members here.
} That provides a namespace that can never be used for anything else. The only valid use of an extension name, other than designating a static namespace, is in an extension resolution override like That's as close to a pure namespace as you can be today. It's pretty darn close too. |
Thanks to both @jakemac53 and @lrhn for the great responses. Enhanced enums fit a lot of use cases that you might want a static class for.
Fortunately it looks like there are plenty of different ways to accomplish what I'm trying to do, so I think we're good to close the issue. |
@lrhn I believe you're thinking about #2254 (comment), as well as a lot of namespace-related talk in #336 |
Thanks @Levi-Lesches, there doesn't seem to be a single issue just about namespaces, but it keeps cropping up in other discussions. The A namespace would be like a |
For those interested, I've created #2272 based on what's been discussed in this issue. |
With the upcoming class modifiers will |
Yeah, I think |
Often times, a class is created as a way to organize a bunch of
static
functions/variables/constants rather than to be instantiated or extended. TheColors
class is a great example: instead of creating objects of typeColors
, you use its static members in place ofColor
objects.If you take a look at the colors.dart file, you'll see that they do something clever:
Since the
abstract
keyword doesn't prevent extension, it's necessary to have this empty private constructor. However, this isn't very intuitive, so you have to include that comment every time you make a static class.Proposal
Allow classes to be defined using the
static
keyword.For example, let's say we wanted to make a collection of
Shape
objects.We could do something like this:
But the class would be much more elegant if we could write it another way:
This feature already exists in languages like C#, and I believe it would be a great addition to the Dart language.
The text was updated successfully, but these errors were encountered: