www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10765] New: Cannot Use Index in Foreach When Iteratee is a Tuple

reply d-bugmail puremagic.com writes:
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



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: -------
Aug 05 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10765


bearophile_hugs eml.cc changed:

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





     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: -------
Aug 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10765






 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: -------
Aug 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10765






 
 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: -------
Aug 06 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10765






 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: -------
Aug 06 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10765





 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: -------
Aug 07 2013