Comment #1 by bearophile_hugs — 2013-08-05T21:18:21Z
(In reply to comment #0)
> foreach(i, left, right; zip("test", "test"))
A solution, that I think is the right one, is to use enumerate(), from Issue 5550 :
foreach (i, left, right; zip("test", "test").enumerate)
(But note that the unpacking of tuples in foreach is a very experimental feature, don't rely too much on it.)
Comment #2 by bearophile_hugs — 2013-08-05T21:21:16Z
(In reply to comment #1)
> foreach (i, left, right; zip("test", "test").enumerate)
This means I think this issue should be closed.
Comment #3 by monkeyworks12 — 2013-08-06T05:25:07Z
(In reply to comment #2)
> (In reply to comment #1)
>
> > foreach (i, left, right; zip("test", "test").enumerate)
>
> This means I think this issue should be closed.
It's not yet a part of the standard library, however.
Comment #4 by bearophile_hugs — 2013-08-06T06:47:12Z
(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);
}
Comment #5 by monkeyworks12 — 2013-08-07T14:20:46Z
(In reply to comment #4)
> 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.
I think this should be considered a bug in the language. I'd expect the tuple version of foreach to work the same as in any other situation, i.e., allow one to use an index.
> 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.
Again, it's only with tuples that foreach with an index works differently. It's not orthogonal at all to have to use enumerate in one special case.
> 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:
I'm currently using a workaround, but I think this is a bug. I'd like to get another opinion on this.