"tuple unpacking" with zip?

Shriramana Sharma via Digitalmars-d digitalmars-d at puremagic.com
Wed Oct 21 03:08:18 PDT 2015


In Python I can do:

ints = [1, 2, 3]
chars = ['a', 'b', 'c']
for i, c in zip(ints, chars):
    print(i, c)

Output:

1 a
2 b
3 c

But in D if I try:

import std.stdio, std.range;
void main ()
{
    int [] ints = [1, 2, 3];
    char [] chars = ['a', 'b', 'c'];
    foreach(int i, char c; zip(ints, chars))
        writeln(i, ' ', c);
}

I get at the foreach line:

Error: cannot infer argument types

But if I read the grammar at 
http://dlang.org/statement.html#ForeachStatement correctly, the foreach 
syntax does permit any number of identifiers, so I'm guessing that the 
limitation is with http://dlang.org/phobos/std_range.html#zip which says 
items can only be accessed by indexing.

What would be needed to std.range.Zip to get the expected functionality?

-- 
Shriramana Sharma, Penguin #395953


More information about the Digitalmars-d mailing list