The most awesome "forward to member" solution?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sun May 3 18:01:16 PDT 2015


On 5/3/15 5:29 PM, Meta wrote:
> On Sunday, 3 May 2015 at 23:54:49 UTC, Andrei Alexandrescu wrote:
>> On 5/3/15 4:20 PM, Meta wrote:
>>> On Sunday, 3 May 2015 at 20:25:45 UTC, Andrei Alexandrescu wrote:
>>>> OK, here's what I have now - two templates that are self contained and
>>>> work well:
>>>>
>>>> The first uses opDispatch to dispatch to a member. The second is a
>>>> simple string function that generates the appropriate code. As
>>>> discussed the latter composes but the former doesn't.
>>>
>>> I can't picture what you're talking about. Could you post an example?
>>
>> struct A
>> {
>>    void fun(int);
>> }
>>
>> struct B
>> {
>>     A a;
>>     mixin forwardToMember!(a, "fun");
>> }
>>
>> struct C
>> {
>>     B b;
>>     mixin forwardToMember!(b, "fun");
>> }
>>
>>
>> Andrei
>
> I still don't get it. Your example works with both dispatchToMember and
> forwardToMember. What is the difference between these two exactly?
>
> auto opDispatch(string sym: foo)(ParemeterTypeTuple!(/*etc*/) args)
> {
>      return parent.foo(args);
> }
>
> //Automatically generated
> auto foo(ParameterTypeTuple!(/*etc*/) args)
> {
>      return parent.foo(args);
> }

Hmm, I didn't try it assuming it won't work (thought __traits(getMember, 
member, sym) would fail with opDispatch). Tried it just now, it does 
work like a charm. Thanks!

Andrei



More information about the Digitalmars-d mailing list