On Wednesday, 21 October 2015 at 10:08:24 UTC, Shriramana Sharma wrote:
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?

static assert(is(ElementType!string == dchar));

    foreach(int i, dchar c; zip(ints, chars))
or
    foreach(i, c; zip(ints, chars))

will work fine.

Reply via email to