[Issue 4347] New: foreach over range should save range.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jun 19 08:49:44 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4347

           Summary: foreach over range should save range.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: dsimcha at yahoo.com


--- Comment #0 from David Simcha <dsimcha at yahoo.com> 2010-06-19 08:49:42 PDT ---
To be consistent with the old definition of forward ranges, and with arrays and
opApply-based ranges, a foreach loop should call save() if it's available.  The
example below demonstrates why not doing so is problematic.

import std.stdio;

class SomeRange {
    uint num;

    uint front() {
        return num;
    }

    void popFront() {
        num++;
    }

    bool empty() @property {
        return num >= 10;
    }

    typeof(this) save() @property {
        auto ret = new typeof(this);
        ret.num = num;
        return ret;
    }
}

void main() {
    auto r = new SomeRange;
    foreach(elem; r) {
        writeln(elem);  // Prints numbers 0-9.
    }

    foreach(elem; r) {
        writeln(elem); // Nothing.
    }
}

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


More information about the Digitalmars-d-bugs mailing list