Referring to alias parameters in a mixin template

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 17 04:49:09 PST 2014


On Wednesday, 17 December 2014 at 02:34:16 UTC, aldanor wrote:
> On Wednesday, 17 December 2014 at 02:12:52 UTC, anonymous wrote:
>> Sure, straight forward:
>>
>>      mixin template makeProperty(T, string name, alias func) {
>>          mixin("T %s() @property { return func();
>> }".format(name));
>>      }
>
> Indeed... thanks! Just one thing that I find confusing here -- 
> how
> exactly do T and func resolve when this template is mixed in?
> What if there was another local symbol named "func" in the 
> target
> scope?

As far as I understand, the string mixin is resolved first, and
then the template mixin takes place. So the progression is
somewhat like this (pseudo code):

----
mixin makeProperty!(int, "foo", f);

/* Replace "makeProperty" with its definition. */
mixin (T, name, func){mixin("T %s() @property { return
func();}".format(name));}!(int, "foo", f);

/* First round, substitute arguments for parameters. `T` and
`func` are not replaced, because they're just string contents at
this point. */
mixin (T, name, func){mixin("T %s() @property { return
func();}".format("foo"));}!(int, "foo", f);

/* Evaluate `format` and do the string mixin. */
mixin (T, name, func){T foo() @property { return func();}}!(int,
"foo", f);

/* Second round, substitute arguments for parameters. This time,
`T` and `func` are replaced. */
mixin (T, name, func){int foo() @property { return f();}}!(int,
"foo", f);

/* Didn't do any string mixins in the second round, so there's no
need for a third round. Get rid of template parameters and
arguments. */
mixin {int foo() @property { return f();}};

/* Finally, do the template mixin. */
int foo() @property { return f();}
----

Not sure if that helps or maybe it just adds to the confusion.

As to if there were `T` or `func` in the target scope, they'd be
shadowed by the template parameters, no matter if there's a
string mixin or not.


More information about the Digitalmars-d-learn mailing list