A template for method forwarding?

Bill Baxter wbaxter at gmail.com
Fri Dec 12 13:10:32 PST 2008

On Sat, Dec 13, 2008 at 6:00 AM, Simen Kjaeraas <simen.kjaras at gmail.com> wrote:
> On Fri, 12 Dec 2008 21:08:51 +0100, Bill Baxter <wbaxter at gmail.com> wrote:
>> Let's say you want to use object composition instead of inheritance.
>> Now you want to forward half-a-dozen method from the new to class to
>> the composed class, like so:
>> class NewClass
>> {
>>     ImplT implementor;
>>     ...
>>     // Do some method forwarding
>>     void func1(int a, float b) { implementor.func1(a,b); }
>>     string func2(string s) { return implementor.func2(s); }
>>     T aTemplate(T)(T val, T[] arr) { return implementor!(T)(val,arr); }
>>     ...
>> }
>> It becomes pretty tedious to type all these things out, and if the
>> base class changes a method signature, you have to remember to do it
>> in the parent class too.
>> So the challenge is to write some kind of template that does the
>> necessary argument deduction to implement a forwarder just by
>> mentioning the name of the method and the object to forward to.
>> Something like this perhaps for the usage syntax:
>>    mixin call_forward!(implementor, "func1");
>>    mixin call_forward!(implementor, "func2");
>>    mixin call_forward!(implementor, "aTemplate");
>> Is it possible?  Somebody must have done something like this already.
>> --bb
> Wouldn't opDot do what you want here?

Not really.  I don't want to willy nilly forward all method to
implementor.  I may want to hide some methods, or I may be composing
two different objects and want to forward some methods to one and some
to the other.


More information about the Digitalmars-d mailing list