Having mirrors returning futures dramatically limits what you can do with them.
For example,
class ObjectA {
methodA(){}
methodB(){}
}
class DynamicWrapper extends MagicalDynamicWrapper {
ObjectA real;
Map<String, Function> methods;
DynamicWrapper(this.real, this.methods);
}
var w = new DynamicWrapper(new ObjectA(), {"methodB" : (){ 'do something' }});
w.methodA() // calls ObjectA.methodA
w.methodB() // calls 'do something'
The way I'd normally do it is by defining noSuchMethod in MagicalDynamicWrapper:
Unfortunately, calling the real object will always return a future. So it doesn't work.
At some point it was possible to get the value of a Future (using the value getter), but that getter is no longer available.
My Question:
Is there a way to get the result of a reflection call synchronously?
In a distributed setting futures are definitely the way to go. But in a non-distributed setting, where all the meta information is available, it should be possible to get the value of a reflection call. I'd make a huge difference for the authors of testing frameworks and build tools.
Having mirrors returning futures certainly makes sense
No it doesn't (well, at least not to me :-) ). And it will probably be changed, see http://dartbug.com/4633#c17
The way I'd normally do it is by defining noSuchMethod in MagicalDynamicWrapper that will check if there is a method with such a name in the methods map, then call it, and if there is none, it will call the "real" object via reflection.
Check the InvocationMirror
documentation. You can forward method calls using the invokeOn
method:
class Proxy {
var target;
Map<String, Function> overrides;
Proxy(this.target, this.overrides);
noSuchMethod(invocation) {
if (overrides.containsKey(invocation.memberName)) {
return overrides[invocation.memberName]();
} else {
return invocation.invokeOn(target);
}
}
}
Is there a way to get the result of a reflection call synchronously?
No, it isn't. Not right now. You can star http://dartbug.com/4633 if you wish.