use case for "alias this"

spir denis.spir at gmail.com
Wed Apr 20 02:44:58 PDT 2011


Hello,

For what it's worth, I have finally found one use case for the famous "alias 
this" trick for structs:

struct ScienceConstant {
     float value;
     string name;
     alias value this;
     string toString () { return this.name; }
}

unittest {
     auto PI = ScienceConstant(3.14, "pi");
     writefln("%s/3 = %s", PI, PI/3);
     // --> pi/3 = 1.04667
}

For people who don't know it: the point is that "alias this" implicitely 
delegates messages (here requests for arithmetic operations) to the actual 
element (here value) on which they should apply, that happens to be embedded in 
a struct object. Or rather: that's how I conceive this feature.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list