static foreach direct use of variables

Paul Backus snarwin at gmail.com
Tue Jan 1 21:34:08 UTC 2019


On Tuesday, 1 January 2019 at 21:14:09 UTC, Michelle Long wrote:
> auto foo(S s)
> {
> 	static foreach(k, p; [s, this])
> 		for(int i = 0; i < p.length; i++)
>         		...
> }
>
> The idea is to provide single for loop structure for each of 
> the variables(in this case s and this).

You can do this with `std.meta.AliasSeq`:

     string[] a = ["lions", "tigers", "bears"];
     int[] b = [123, 456, 789];

     static foreach(array; AliasSeq!(a, b)) {
         foreach (item; array) {
             writeln(item);
         }
     }


More information about the Digitalmars-d-learn mailing list