Explicitly passing `this` to methods

Tomer Filiba via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 19 02:16:06 PDT 2014


Is there a way to explicitly pass `this` instance to a method? 
Consider the following example:

-------
class Foo {
     void f(int a) {}
}

struct Caller(alias F) {
     mixin(__traits(parent, F).stringof ~ " inst;");

     auto call(ParameterTypeTuple!F args) {
         return F(args);
     }
}

void main() {
     auto c = Caller!(Foo.f)(new Foo);
     c.call(6);
}
-------

It fails with "Error: this for f needs to be type Foo not type 
Caller!(f)", because it takes `this` from the the struct and 
tries to apply it to the method.

I tried `F(inst, args)` or `inst.F(args)` but they don't work... 
what I need is the inverse of UFCS (e.g., `func(inst,1,2,3) ==> 
inst.func(1,2,3)`)

Is there any way to overcome this?

Thanks,
-tomer


More information about the Digitalmars-d mailing list