Ranges and indexes with foreach

kenji hara k.hara.pg at gmail.com
Mon Jan 23 17:48:32 PST 2012


2012/1/24 bearophile <bearophileHUGS at lycos.com>:
>> import std.stdio, std.range, std.algorithm;
>> void main() {
>>     auto range = [1, 2, 3, 4];
>>     foreach (i, e; zip(sequence!"n"(), range))
>>         writeln(i, " ", e);
>> }
>
> But I was talking about tuple unpacking, not typetuple unpacking:

std.range.zip makes a range of std.typecons.Tuple, not std.typetuple.TypeTuple.
Then foreach statement supports the std.typecons.Tuple unpacking of range.front.

> import std.stdio, std.range, std.algorithm, std.typecons;
> alias Tuple!(int,int) P;
> void main() {
>    auto ap = [P(1,2), P(3,4), P(5,6)];
>    foreach ((x, y); ap)  // <== std.typecons.tuple unpacking
>        writeln(x, " ", y);
> }

Therefore, follows would work.

    auto ap = [P(1,2), P(3,4), P(5,6)];
    foreach (x, y; ap)  //
        writeln(x, " ", y);

Kenji Hara


More information about the Digitalmars-d mailing list