Proxy

Creates a proxy for the value a that will forward all operations while disabling implicit conversions. The aliased item a must be an lvalue. This is useful for creating a new type from the "base" type (though this is not a subtype-supertype relationship; the new type is not related to the old type in any way, by design).

The new type supports all operations that the underlying type does, including all operators such as +, --, <, [], etc.

mixin template Proxy () {}

Parameters

a

The value to act as a proxy for all operations. It must be an lvalue.

Examples

1 struct MyInt
2 {
3     private int value;
4     mixin Proxy!value;
5 
6     this(int n){ value = n; }
7 }
8 
9 MyInt n = 10;
10 
11 // Enable operations that original type has.
12 ++n;
13 assert(n == 11);
14 assert(n * 2 == 22);
15 
16 void func(int n) { }
17 
18 // Disable implicit conversions to original type.
19 //int x = n;
20 //func(n);

The proxied value must be an lvalue.

1 struct NewIntType
2 {
3     //Won't work; the literal '1'
4     //is an rvalue, not an lvalue
5     //mixin Proxy!1;
6 
7     //Okay, n is an lvalue
8     int n;
9     mixin Proxy!n;
10 
11     this(int n) { this.n = n; }
12 }
13 
14 NewIntType nit = 0;
15 nit++;
16 assert(nit == 1);
17 
18 
19 struct NewObjectType
20 {
21     Object obj;
22     //Ok, obj is an lvalue
23     mixin Proxy!obj;
24 
25     this (Object o) { obj = o; }
26 }
27 
28 NewObjectType not = new Object();
29 assert(__traits(compiles, not.toHash()));

There is one exception to the fact that the new type is not related to the old type. Pseudo-member functions are usable with the new type; they will be forwarded on to the proxied value.

1 import std.math;
2 
3 float f = 1.0;
4 assert(!f.isInfinity);
5 
6 struct NewFloat
7 {
8     float _;
9     mixin Proxy!_;
10 
11     this(float f) { _ = f; }
12 }
13 
14 NewFloat nf = 1.0f;
15 assert(!nf.isInfinity);

Meta

Suggestion Box / Bug Report