What is the correct way to forward method calls to the struct field?

Philippe Sigaud via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 21 13:19:49 PDT 2014


You can use 'auto' to let the compiler deduce the return type. Here I
use 'foo' which returns an int and 'bar', which returns void.

struct SomeT {
    int foo(double d) { return 0;}
    void bar(double d) { return;}
}

struct S {
    auto /* here */
       opDispatch(string name, Args...)(Args args) {
        return mixin("(*iface)." ~ name)(args);
    }
    SomeT** iface;
}


void main()
{
  SomeT st;
  SomeT* st_p = &st;
  SomeT** st_p_p = &st_p;

  S s = S(st_p_p);

  import std.stdio : writeln;
  writeln(s.foo(3.14)); // "0"
  writeln(typeof(s.bar(3.14)).stringof); // "void"
}

On Sat, Jun 21, 2014 at 8:21 PM, monnoroch via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:
> On Saturday, 21 June 2014 at 18:16:17 UTC, monnoroch wrote:
>>
>> Actyaly, last variant didn't work either: i was testing it
>> inside S like "ifc.foo(1)", so UFCS kiked in, not alias this.
>
>
> Oh, my bad, "alias impl this;" actually did work. But the first
> argument problem still holds.


More information about the Digitalmars-d-learn mailing list