Skip to content
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

Closed
jmesserly opened this issue Aug 28, 2012 · 8 comments
Closed

noSuchMethod for static calls, new #4790

jmesserly opened this issue Aug 28, 2012 · 8 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug

Comments

@jmesserly
Copy link

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());
}

@jmesserly
Copy link
Author

fwiw, this would help JS interop


cc @vsmenon.

@lrhn
Copy link
Member

lrhn commented Aug 29, 2012

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 :)

@jmesserly
Copy link
Author

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.

@gbracha
Copy link
Contributor

gbracha commented Oct 14, 2013

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.


Set owner to @gbracha.
Added Accepted label.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@jmesserly jmesserly added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Aug 4, 2014
@jmesserly
Copy link
Author

I think metaclass DEP would support this?
Anyway, I'm happy to close. This issue is pretty old by this point.

@jiridanek
Copy link

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?

@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants