Skip to content

Runtime dynamic object/real proxy #23896

@BlackHC

Description

@BlackHC

It is very useful that we can override noSuchMethod and create effective proxies. It would be even more useful if it were possible to create proxies that implement every interface (actual dynamics) when it comes to type checks.

// Implements every interface in regard to type-checks
class Forwarder implements dynamic {
  final _delegate;

  Forwarder(this._delegate);

  noSuchMethod(invocation) => ...;
}
Forwarder forwarder = new Forwarder('hello');
String a = forwarder;
assert(a.runtimeType == Forwarder);
assert(a is String);

Forwarder is effectively dynamic. Currently, this only works for generic parameters in type checks.
(print(new List<dynamic>() is List<String>); // prints true).

An alternative would be to provide a way to mixin classes at runtime, but that would seem much more difficult to implement in the VM/dart2js.

Currently, one has to manually create proxies that implement the right interfaces to avoid runtime errors, which does not scale well for writing generic interceptors/loggers/etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.closed-obsoleteClosed as the reported issue is no longer relevanttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions