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
noSuchMethod for static calls, new #4790
Comments
fwiw, this would help JS interop cc @vsmenon. |
The noSuchMethod catch is for dynamic invocations, where you don't know the class of the object at compile time. That means that you can sneak in an object without knowing before-hand what you need to implement, and catch it with a noSuchMethod. All static invocations, and constructor invocations, in Dart are currently statically resolved, so you know the exact class and method at compile time. I'm not saying we couldn't do something similar, but I can't see the use of it - if you can add a noSuchMethod to the class, why can't you add the exact function you want to call too. The only difference I can see is that noSuchMethod can implement an infinite number of methods in finite space. I'm not sure I like the idea of an infinite number of static methods on a class :) |
The use case is something like: you're writing in a library and you don't know your what names your class will be called with. Here's an example: new JS.Map() Here, we're doing interop. The JS class doesn't know there should be a "Map" constructor until runtime. It is in some sense supporting an "infinite" number of methods--the set of methods is not known at compile time. I think it's very similar to the normal noSuchMethod use case: supporting calls to names that aren't available statically. |
This would be nice if it wasn't such a special case. If static methods were treated as instance methods on class objects, then the problem would go away for statics. And if we really followed uniform reference, we wouldn't have a special syntax for constructor calls, they'd be just like static methods. There are languages that do this of course. To what extent can we retrofit this into Dart? Unclear. |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
I think metaclass DEP would support this? |
I am not sure I see how metaclass DEP (https://github.com/gbracha/metaclasses/blob/master/proposal.md, as reference for people finding this issue first) supports this. Would I then be able to define a static method noSuchMethod on my class, which would then become the regular method noSuchMethod on the metaclass? And failed static method lookups will be redirected to calling it? |
Right now it doesn't seem possible to intercept constructor calls or static calls via noSuchMethod.
Is that intended? Of course, noSuchMethod on the instance wouldn't work, but I'd expect to be able to hook it on the class somehow (and libraries eventually :) )
class Foo {
bar() => print('its a bar');
noSuchMethod(name, args) => 'NSM $name';
// not legal: static noSuchMethod(name, args) => 'NSM $name';
}
void main () {
print(new Foo().baz);
// how to intercept this? print(Foo.quux);
// or this? print(new Foo.fisk());
}
The text was updated successfully, but these errors were encountered: