Another interesting hack: expand a static array into parameter arguments

monarch_dodra monarchdodra at gmail.com
Thu Apr 3 05:30:26 PDT 2014


On Thursday, 3 April 2014 at 08:59:44 UTC, Andrej Mitrovic wrote:
> On 4/2/14, Artur Skawina <art.08.09 at gmail.com> wrote:
>>    template expand(alias A, alias M=Delay) {
>>       mixin(q{alias expand = TypeTuple!(}
>>              ~ 
>> iota(A.length).map!q{",M!(A,"[!a..$]~text(a)~")"}().join()
>>              ~ q{);});
>>    }
>
> You can always use string mixins for these hacks. But they are
> terrible to debug, and likely slower to compile. I can barely 
> make out
> what the above code does even though I deal with mixins and 
> templates
> every day.

I agree that you should avoid string mixins when you can, 
however, with enough tweaking, you can sometimes format it into 
something readable.

I've been playing around a lot with format and range formatting, 
and I find the results to be "almost readable":

                                   //(  M!(A,i), ... )
enum s = q{alias expand = TypeTuple!(%(M!(A,%s)%|,%));}
     .format(iota(A.length));


More information about the Digitalmars-d mailing list