D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10765 - Cannot Use Index in Foreach When Iteratee is a Tuple
Summary: Cannot Use Index in Foreach When Iteratee is a Tuple
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-08-05 17:34 UTC by Meta
Modified: 2015-12-27 02:44 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Meta 2013-08-05 17:34:16 UTC
import std.range;

void main()
{
        //Error: cannot infer argument types
	foreach(i, left, right; zip("test", "test"))
	{
		assert(left == right);
	}
}
Comment 1 bearophile_hugs 2013-08-05 21:18:21 UTC
(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 bearophile_hugs 2013-08-05 21:21:16 UTC
(In reply to comment #1)

> foreach (i, left, right; zip("test", "test").enumerate)

This means I think this issue should be closed.
Comment 3 Meta 2013-08-06 05:25:07 UTC
(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 bearophile_hugs 2013-08-06 06:47:12 UTC
(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 Meta 2013-08-07 14:20:46 UTC
(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.