On Sat, Aug 4, 2012 at 3:04 PM, Timon Gehr <timon.g...@gmx.ch> wrote:

>> This should be
>> explained somewhere, as it's a tool anyone using templates to do
>> metaprogramming should be aware of.
>>
>
> http://d.puremagic.com/issues/show_bug.cgi?id=7653

Yes. There is a workaround, but we lose the elegance of the previous solution.

mixin template AddMembers()
{
    private static string __AddMembers()
    {
        string r;
        foreach(x;["a","b","c"]) r~="int "~x~";";
        return r;
    }
    mixin(__AddMembers());
}

struct S
{
    mixin AddMembers;
}

void main()
{
    S s;
    writeln(s.a);
}

The limit is of course this introduce a new symbol in the local scope
(here, __AddMembers, made private and static).
Not that the collision risk with user code is great, the catch is if a
second instantiation of an equivalent template is done, thus trying to
create in the scope an already defined symbol.

Hence my need for a gensym template I alluded to in a previous
message. AddMembers would first create a fresh, non-already-used-here
symbol and sue that as its helper function's name.

And... drat, my gensym use the same trick, so back to the starting
point. I'll vote this bug up, one never knows.

Reply via email to