Add language-level support for object forwarding #20705
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
This issue was originally filed by @yissachar
Object delegation is a common solution when inheritance is not an option. For instance, before ListBase was introduced, one option to get around inheriting List was to use a delegate list and then forward all the method calls. Forwarding the method calls can be done manually, which is tedious. Alternatively, forwarding can quickly be accomplished with noSuchMethod at the cost of tooling support (since the analyzer no longer can verify your calls) and potential code size issues when deploying to JavaScript.
Language-level support for this common pattern would provide the advantages of manual delegation without forcing the user to type out every method.
One possible implementation would be through a "proxy" keyword or something similar that would indicate a field is to be delegated.
class MyList {
proxy _list = [0, 1, 2];
}
var myList = new MyList();
myList.add(3);
print(myList.length);
Multiple proxies for a class would not be allowed if they would cause ambiguity, but could be allowed otherwise (e.g. proxying two Lists would not be allowed since there would be no way to determine which to delegate to, but proxying List alongside Foo which does not share any method signatures would be fine).
The text was updated successfully, but these errors were encountered: