alias to a property as an argument to a mixin template

comco void.unsigned at gmail.com
Sun Sep 23 11:49:10 PDT 2012


For this program I'm getting an "Error: need 'this' to access 
member x" at line (*). Does that mean that we cannot alias a 
property as an argument of a template mixin?

     import std.stdio;

     mixin template T(alias a) {
         void f() {
             writeln(a); // (*)
         }
     }

     struct S {
         int x;
     }

     void main() {
         auto s = S(4);
         mixin T!(s.x);
         f();
     }

If I change main() as follows, I still get the same error:

     void main1() {
         auto s = S(4);
         with (s) {
             mixin T!(x);
             f();
         }
     }

But now, I can get it working by this trick:

     import std.stdio;

     mixin template T(alias a) {
         void f() {
             mixin("writeln(" ~ a.stringof ~ ");");
         }
     }

     struct S {
         int x;
     }

     void main() {
         auto s = S(4);
         with (s) {
             mixin T!(x);
             f();
         }
     } // prints 4

So, using string mixins works, but explicit alias to the property 
name seems not to. Why is that and is there any other way of 
achieving the result witout using template mixins?





More information about the Digitalmars-d-learn mailing list