Create a proxy

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 19 18:05:46 PST 2016


On 02/19/2016 06:18 AM, Voitech wrote:

 > I was trying to use mixins for this i
 > created some functions but this not working don't know why

Compilation errors or something else? pragma(msg) helps with debugging 
generated code. You can also use unittest blocks to make sure that the 
generated code is what you expect.

 > There is also a Proxy template in std.typecons but i don't understand
 > how it works

That one makes a user-defined type to be used as another type. The 
example from the documentation is MyInt behaves exactly like an int by 
forwarding int operations to 'value':

struct MyInt
{
     private int value;
     mixin Proxy!value;

     this(int n){ value = n; }
}

MyInt n = 10;

Support for all the following operations (++, ==, *, and more) are mixed 
in by Proxy:

// Enable operations that original type has.
++n;
assert(n == 11);
assert(n * 2 == 22);

Ali



More information about the Digitalmars-d-learn mailing list