www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4347] New: foreach over range should save range.

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



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: -------
Jun 19 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4347


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 09 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4347


Torarin <torarind gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |torarind gmail.com



I think save() should be required, since the semantics of foreach when
iterating over arrays imply a save() on the array. Letting forward range
behaviour depend on whether the range is a class or a struct is bound to be
confusing.

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