dart-lang / language

Design of the Dart language
Other
2.65k stars 202 forks source link

Dart will support dynamic proxy? #1197

Open Silentdoer opened 4 years ago

Silentdoer commented 4 years ago

Like java's Proxy, it is useful for some framework achieve

lrhn commented 4 years ago

It's not impossible, but it's not something that is part of our short- or medium-term plans.

Dart has noSuchMethod which allows you to statically create a proxy for any type as:

class FooProxy imlements Foo {
   final dynamic Function(Invocation) _handler;
   FooProxy(this._handler);
   noSuchMethod(i) => _handler(i);
}

It is not possible to create a dynamic proxy where the type is not known until run-time. It requires the platform to be able to dynamically create something like the class of FooProxy, on request, most likely based on a type variable. Example API:

external T dynamicProxy<T>(dynamic Function(Invocation) handler);

If the dynamicProxy also supports function types, then it would be extra useful.