[Issue 10765] Cannot Use Index in Foreach When Iteratee is a Tuple

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 6 06:47:13 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10765



--- Comment #4 from bearophile_hugs at eml.cc 2013-08-06 06:47:12 PDT ---
(In reply to comment #3)

> It's not yet a part of the standard library, however.

I know, but you are asking for a change in the D language. It's much simpler to
add a small range to Phobos or to your project, than to change a language. What
you are asking for is also not explicit.

Take a look at Python:


>>> pairs = [(10, 20), (30, 40)]
>>> for a,b in pairs: print a, b
...
10 20
30 40
>>> for i, (a,b) in enumerate(pairs): print i, a, b
...
0 10 20
1 30 40


Why they didn't modify the for iteration? They have added a function like
iterate because it's more orthogonal, allows to keep the language simpler and
more clean, and it's more explicit in what the programmer wants.

So my suggestion it to close this issue down, and to ask people to add an
enumerate() to Phobos, and in the meantime to use some workaround, like:
- define an enumerate() in your code;
- use a index variable that you increment inside the foreach loop
- use something else in std.range to simulate an enumerate, like:

void main() {
    import std.range, std.stdio;

    foreach (i, left, right; sequence!"n"(0).zip("test", "test"))
        writeln(i, " ", left, " ", right);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list