View mode: basic / threaded / horizontal-split · Log in · Help
October 27, 2009
[Issue 3444] New: foreach(i, elem; range) should work
http://d.puremagic.com/issues/show_bug.cgi?id=3444

          Summary: foreach(i, elem; range) should work
          Product: D
          Version: 2.035
         Platform: Other
       OS/Version: Windows
           Status: NEW
         Severity: enhancement
         Priority: P2
        Component: DMD
       AssignedTo: nobody@puremagic.com
       ReportedBy: dsimcha@yahoo.com


--- Comment #0 from David Simcha <dsimcha@yahoo.com> 2009-10-26 18:38:59 PDT ---
Currently, one cannot do a foreach statement over a range that also gives the
index.  This is inconsistent with arrays.  I'm not sure if it's the best fix,
but at least a temporary fix is to mix this thing into all ranges:

template CountForeach(I) {
   int opApply(int delegate(ref I, ref typeof(this.front())) dg) {
       I index = 0;
       int result;
       foreach(elem; this) {
           result = dg(index, elem);
           if(result) {
               break;
           }
           ++index;
       }

       return result;
   }
}

Usage:

struct SomeRange {

   SomeType front() { return something; }

   void popFront() {  
       doStuff();
   }

   bool empty() {
       return amIEmpty();
   }

   mixin CountForeach!size_t;
}

void main() {
   SomeRange someRange;

   foreach(elem; someRange) {}  // Uses range interface directly.

   foreach(i, elem; someRange) {}  // Uses the mixin;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 10, 2009
[Issue 3444] foreach(i, elem; range) should work
http://d.puremagic.com/issues/show_bug.cgi?id=3444


David Simcha <dsimcha@yahoo.com> changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
            Status|NEW                         |RESOLVED
        Resolution|                            |FIXED


--- Comment #1 from David Simcha <dsimcha@yahoo.com> 2009-12-09 20:03:17 PST ---
Fixed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 10, 2009
[Issue 3444] foreach(i, elem; range) should work
http://d.puremagic.com/issues/show_bug.cgi?id=3444


David Simcha <dsimcha@yahoo.com> changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
            Status|RESOLVED                    |REOPENED
        Resolution|FIXED                       |


--- Comment #2 from David Simcha <dsimcha@yahoo.com> 2009-12-09 20:37:20 PST ---
Argh, accidentally marked the wrong bug fixed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 10, 2010
[Issue 3444] foreach(i, elem; range) should work
http://d.puremagic.com/issues/show_bug.cgi?id=3444



--- Comment #3 from Shin Fujishiro <rsinfu@gmail.com> 2010-06-09 23:25:08 PDT ---
Created an attachment (id=659)
patch against dmd r526

Implemented foreach(i, e; r) and foreach_reverse(i, e; r).
For reverse iteration with index, r must have a length property.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Top | Discussion index | About this forum | D home