Ranges and indexes with foreach

bearophile bearophileHUGS at lycos.com
Mon Jan 23 17:08:34 PST 2012


> 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:


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);
}

Bye,
bearophile


More information about the Digitalmars-d mailing list