Template help - MultiAccess
Era Scarecrow
rtcvb32 at yahoo.com
Tue Jan 8 18:42:09 PST 2013
On Tuesday, 8 January 2013 at 23:17:46 UTC, Philippe Sigaud wrote:
> The 'int,' part is not necessary: names a and b have a type,
> you can determine int from CommonType!(typeof(a),typeof(b)).
> One less field for your user.
Here's a working example, although my updated code hasn't been
synced yet. Naming isn't used but is small enough you don't
really need them.
struct S {
struct X {int d, e;}
X x;
mixin(multiAccess!(int, "maxX", "@safe pure nothrow",
"x.d > x.e", true, true, //choice, read/write
"x.d", true,
"x.e", false));
}
unittest {
S s;
s.x.d = 5;
s.x.e = 10;
//maxX returns and references ONLY the larger of the two.
assert(s.maxX == 10);
s.maxX = 15;
assert(s.maxX == 15);
assert(s.x.d == 5 && s.x.e == 15);
}
In this case, d & e are both strings, and returning a string
wouldn't work (with an int). Choice can be replaced with a
function (or lambda?).
I think it's clear to see how these can be used.
More information about the Digitalmars-d-learn
mailing list