alias to a property as an argument to a mixin template
Ali Çehreli
acehreli at yahoo.com
Sun Sep 23 13:44:52 PDT 2012
On 09/23/2012 11:49 AM, comco wrote:
> 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?
>
>
>
There is also delegates:
import std.stdio;
struct S {
int x;
}
void main() {
auto s = S(4);
auto f = { return s.x; };
f();
}
Ali
More information about the Digitalmars-d-learn
mailing list