-
Notifications
You must be signed in to change notification settings - Fork 214
[Feature Request]: Access Static Variables and Methods on an Instance #4302
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
Comments
From previous discussion:
Only way I can see this working is if Would still be a breaking change if using Since I don't see any useful scenario of this, this proposal are still getting a thumps down from me. |
We don't allow a static member of a type to have the same name as a dynamic member, so this should be trivial to implement. I would rather allow it, however. I'm pretty sure there's a sub-discussion about this somewhere, but I can't find it now. |
One way to provide a similar effect would be to have some general type-level functions (evaluated at compile-time, relying on the static analysis). For example, we could have something which is similar to the C++ feature class ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName {
static String staticValue = "Hello, Dart!";
static void staticMethod() => print("Static method called!");
}
void main() {
var instance = ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName();
print(typeof(instance).staticValue);
typeof(instance).staticMethod();
} When
void main() {
var xs = <int>[];
typeof(xs) ys;
...
ys = <typeof(xs)?>[null];
...
} The syntax |
Would you be able to create a instance member that points to a static class using final instance = ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName();
final staticInstance = typeof(instance);
staticInstance.staticMethod(); // Work fine |
typedef F = ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName; So (That said, #4200 and #4301 would allow us to make |
More realistically, I think this would be valuable ... for extension methods: extension on ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName {
void myMethod() {
print(staticValue); // Expected: "Hello, Dart!"
staticMethod(); // Expected: "Static method called!"
}
} |
Don't forget typedef EWRealyLN = ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName;
class ExampleWithARealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyRealyLongName {
static String staticValue = "Hello, Dart!";
static void staticMethod() => print("Static method called!");
}
void main() {
print(EWRealyLN.staticValue); // Expect: "Hello, Dart!"
EWRealyLN.staticMethod(); // Print: "Static method called!"
} |
Etude: class A {
static A staticMethod()=>A();
}
void main() {
var a=A();
var aa=([a]..add(.staticMethod()))[1];
} Maybe a shorter version exists, but I couldn't find it. |
This required every class to have a empty constructor... |
I don't understand the connection to empty constructor, sorry. The etude was supposed to illustrate the interplay between different features. It's an argument in favor of adding |
I expect that to change at some point. With extensions, and even more if allowing static extension members (#723), you can already have something that looks like the same name as both a static member and an instance member. For this request, there is nothing it allows which you can't write without this feature. The type that the static is called on is statically known, so you could just write it. The names of types, which you use to access static members, are also access controls. Allowing you to access statics without writing the name bypasses that access control. That's true for dot-notation too, so that ship has probably sailed. Just don't expose your private types in your public API, and you should be fine. |
Currently, in Dart, static variables and methods belong to the class itself and cannot be accessed through an instance of the class. This can sometimes lead to unnecessary boilerplate when attempting to access shared class-level functionality through an instance.
Allow access to static variables and methods on an instance of a class, similar to how some other languages (e.g., JavaScript) allow this behavior. For example:
Benefits
Improves developer experience by reducing friction when accessing static members.
Eliminates unnecessary referencing of the class name, making code more concise.
Possible Concerns
May introduce ambiguity in cases where instance-level properties/methods have similar names to static ones.
Vote: 👍 for
yes
and 👎 forno
Ref issue
The text was updated successfully, but these errors were encountered: