static code generation

anonymous anonymous at example.com
Sun Dec 9 12:13:00 PST 2012


On Sunday, 9 December 2012 at 19:54:17 UTC, js.mdnq wrote:
> On Sunday, 9 December 2012 at 19:34:05 UTC, anonymous wrote:
>> On Sunday, 9 December 2012 at 19:24:24 UTC, js.mdnq wrote:
>>>> In this particular case you can do this:
>>>> mixin template GenStruct(string stringname)
>>>> {
>>>>   struct S
>>>>   {
>>>>       ....
>>>>   }
>>>>   mixin("alias S " ~ stringname ~ "alpha;");
>>>> }
>>>
>>> But what if I use more than one mixin? I'll have multiple 
>>> structs
>>> with the same name.
>>
>> That's not an issue.
>>
>> "If two different mixins are put in the same scope, and each 
>> define a declaration with the same name, there is an ambiguity 
>> error when the declaration is referenced" (note: when it's 
>> referenced, not when it's declared)
>> and
>> "If the name of a declaration in a mixin is the same as a 
>> declaration in the surrounding scope, the surrounding 
>> declaration overrides the mixin one"
>> - http://dlang.org/template-mixin.html
>
>
> Huh?
>
> No, I mean, if I use the mixin more than once?
>
> GenStruct("s1");
> GenStruct("s2");
>
> then I'll have
>
> struct s { } alias s s1;
> struct s { } alias s s2;
>
> which will fail or not be what I want, since I want to generate 
> the structs s1 and s1.

It won't fail and you'll get s1alpha and s2alpha.

Maybe it helps if you see it in action: 
http://dpaste.dzfl.pl/5d4cb742 (Note: "Result: Success", i.e. the 
asserts hold)


More information about the Digitalmars-d-learn mailing list