String mixins with string arrays

Mike James foo at bar.com
Fri Dec 13 05:23:23 PST 2013


On Friday, 13 December 2013 at 12:09:59 UTC, John Colvin wrote:
> On Friday, 13 December 2013 at 11:40:35 UTC, Mike James wrote:
>> Hi,
>>
>> Is it possible to pass a string array to a string mixin e.g
>>
>> template GenSomething(string foo, string[] bar){
>>  some_kind_of_foreach(br: bar) {
>>    const char[] foo ~ br ~ ";\n";
>>  }
>> }
>>
>> and call:
>>
>> mixin(GenSomething!("A", ["B", "C", "D"]));
>>
>> would generate:
>>
>> A.B;
>> A.C;
>> A.D;
>>
>> Regards,
>>
>> -=mike=-
>
> CTFE is your friend here.
>
> string genSomething(string foo, string[] bar) {
>   string result;
>   foreach(br: bar) {
>     result ~= foo ~ '.' ~ br ~ ";\n";
>   }
> }
>
> mixin(genSomething("A", ["B", "C", "D"]));

Thanks - that worked a treat (just needed a 'return result;').

Regards,

-=mike=-


More information about the Digitalmars-d-learn mailing list