DIP 57: static foreach

Daniel N via Digitalmars-d digitalmars-d at puremagic.com
Tue Nov 3 23:51:44 PST 2015


On Wednesday, 4 November 2015 at 00:17:25 UTC, Timon Gehr wrote:
> This is identical to the existing Seq-foreach:
>
> auto foo(Args...)(){
>     foreach(a;Args){
>         pragma(msg,a.stringof);
>     }
> }
>
> void main(){
>     foo!(1,"2",main,int)();
> }
>

It comes very close... but this breaks down(see below). If it had 
been a real alias, it would have worked, but then again it can't 
always be lowered to an alias in the general case, since alias 
has it's own quirks.

Hmm, I guess the real solution is to fix the quirks of alias in a 
separate DIP, first after that foreach could be changed take 
advantage of it?

auto foo(Args...)(){
     foreach(a;Args){
         pragma(msg, a.stringof);
     }
}
auto bar(Args...)(){
     foreach(const i, _;Args){
         alias arg = Args[i];
         pragma(msg, arg.stringof);
     }
}

void main(){
   int a,b,c;
   foo!(a,b,c)();
   bar!(a,b,c)();
   foo!(1)();
   // bar!(1)();
}



More information about the Digitalmars-d mailing list