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:

  • 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.

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.

其他提示

For the time being you can use deprecatedFutureValue(aFuture). Obviously by the name they're discouraging use of it and it will eventually go away, but I agree that until we get a synchronous mirrors API you absolutely need something like this.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top