String mixins with string arrays

John Colvin john.loughran.colvin at gmail.com
Fri Dec 13 04:09:58 PST 2013


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"]));


More information about the Digitalmars-d-learn mailing list