static map as a type function

Paul Backus snarwin at gmail.com
Thu Sep 24 04:39:43 UTC 2020


On Thursday, 24 September 2020 at 04:07:40 UTC, Stefan Koch wrote:
> On Thursday, 24 September 2020 at 03:51:55 UTC, Paul Backus 
> wrote:
>>
>> template staticMap!(alias F, Args...) {
>>     static if (Args.length == 0)
>>         alias staticMap = AliasSeq!();
>>     else
>>         alias staticMap = AliasSeq!(F!(Args[0]), staticMap!(F, 
>> Args[1 .. $]));
>> }
>>
>
> This code only works because tuples auto expand!
> Without that piece of knowledge the template can't be 
> understood.
> And even then I find it a stretch to say this immediately looks 
> like a loop over a parameter tuple.

I agree that tuple auto-expansion is weird, but what are we 
supposed to do about it, just never use tuples? It's not like the 
type function version gets rid of them either--the alias[] only 
exists inside the function, so you still have to deal with tuples 
if you want to use the result.

As far as "not looking like a loop" goes--of course it doesn't! 
It's not a loop. It's a recursive traversal of a list, same as 
you'd write in any functional language.


More information about the Digitalmars-d mailing list