Thread overview
[Issue 10765] New: Cannot Use Index in Foreach When Iteratee is a Tuple
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10765

           Summary: Cannot Use Index in Foreach When Iteratee is a Tuple
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: monkeyworks12@hotmail.com


--- Comment #0 from monkeyworks12@hotmail.com 2013-08-05 17:34:16 PDT ---
import std.range;

void main()
{
        //Error: cannot infer argument types
    foreach(i, left, right; zip("test", "test"))
    {
        assert(left == right);
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10765


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2013-08-05 21:18:21 PDT ---
(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.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10765



--- Comment #2 from bearophile_hugs@eml.cc 2013-08-05 21:21:16 PDT ---
(In reply to comment #1)

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

This means I think this issue should be closed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10765



--- Comment #3 from monkeyworks12@hotmail.com 2013-08-06 05:25:07 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10765



--- Comment #4 from bearophile_hugs@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: -------
August 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10765



--- Comment #5 from monkeyworks12@hotmail.com 2013-08-07 14:20:46 PDT ---
(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.

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