-
Notifications
You must be signed in to change notification settings - Fork 209
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
Implicitly create static methods as wrappers for instance methods #3786
Comments
Using simply It won't work if we allow static and instance members with the same name in the same class. Otherwise the idea isn't bad. |
Correct, this proposal kind of hinges on that:
I left them out intentionally because they certainly do complicate things. In general, you'd probably be safe if you said "let the instance be the first positional parameter, just like Python's |
@Levi-Lesches This feature request should effectively add the only method reference expression Java has and Dart doesn't yet, right?
|
Sounds like it! |
I have many times wished there was a way to get a closure that calls an instance method with a given parameter. Tear-offs do the opposite: they partially apply the receiver but not the arguments. I fairly frequently wish to partially apply the arguments but not the receiver. This proposal would give you a way to do that. I like it. As @lrhn notes, we could actually use I don't know how comfortable I am with doubling down on the restriction that you can't have an instance and static method with the same name. We might want some slightly different syntax, like |
For now. class Color {
final int red, green, blue;
const Color(this.red, this.green, this.blue) : assert(_isValid(red, green, blue);
}
static extension Colors on Color {
static const red = Color(255, 0, 0);
static const green = Color(0, 255, 0);
static const blue = Color(0, 0, 255);
// others
}
void main() {
print(Color.red.red); // 255
} But that's a workaround for what the user actually wants, which is to have a static and an instance member with the same name. |
Threating everything as static method may work in Python, but I am not a fan of doing it the same way in Dart |
In Python, any instance method is really a static method with a
self
parameter, and the object you call the method on is implicitly passed first to this method. For example:In Dart, we often like to use tear-offs instead of passing closures around. A useful example is the following:
However, it is no longer possible to pass a tear-off if we make the greeting an instance method:
My feature request is to implicitly (or on-demand) make available a static method that's equivalent to the Python version of every instance method. Not advocating for instance methods to be replaced by static methods. But, if there's an instance method named
A.b()
, then there should be a static methodA.$b(A a)
, and it should be possible to refer to it as justA.b
as syntax sugar. In other words:The semantics seem to be well-defined as far as I can tell. The alias of
User.makeGreeting
forUser.$makeGreeting
is safe because there cannot be a static member with the same name as an existing instance member, and using an instance member without an instance is always an error. In other words, the alias strictly adds functionality and doesn't break any existing code or create conflicts. Additionally, the compiler may only decide to generate these if they're directly referenced in the first place. This would allow us to use tear-offs way more frequently, especially in JSON contexts where(obj) => obj.toJson()
is quite common.Edit: One specific case has been pointed out to me where this could cause a conflict:
In cases like these, where
ClassName.methodName
is already a valid identifier and would thus cause a conflict, I would say to not generate the implicit static wrapper, to avoid breaking existing code and causing "magic" behavior. But I'm open to whatever makes more sense on a case-by-case basis as well.The text was updated successfully, but these errors were encountered: