www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12109] New: foreach with class alias this range iteration inconsistency

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12109

           Summary: foreach with class alias this range iteration
                    inconsistency
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: peter.alexander.au gmail.com



10:28:34 PST ---
Sorry for the title, hard to explain in a few words:

class A
{
    int[] a = [1, 2, 3];
    alias a this;
}

void main()
{
    import std.stdio, std.array;

    auto a = new A();
    while (!a.empty)
        a.popFront();
    writeln(a.a); // []

    auto b = new A();
    foreach (_; b)
        {}
    writeln(b.a); // [1, 2, 3]
}


The problem: iterating using popFront() manually gives a different result from
using foreach. The foreach iteration doesn't consume the range, while the
popFront/empty does. Presumably the foreach is making a copy of the aliased
array and iterating that instead of the A object directly.

Expected: foreach should be consistent with using popFront/empty manually and
should consume the range, i.e. the final writeln(b.a) should give []

It is very important that these are consistent otherwise algorithms that change
from using popFront/empty to foreach will change behaviour when the user is
using a class with alias this range. This may explain the variations seen in
Issue 9506 between releases.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 08 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12109


Jakob Ovrum <jakobovrum gmail.com> changed:

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



---

 Sorry for the title, hard to explain in a few words:
This has nothing to do with AliasThis. All forward ranges behave this way with foreach (i.e. foreach operates on a `save`'d copy). When `save` is not necessary (such as in algorithms that have already called `save`), a for-loop should be used, as seen in many places in Phobos. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 08 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12109


Peter Alexander <peter.alexander.au gmail.com> changed:

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



10:45:58 PST ---
Sorry, yes, you're right. I'm not thinking.

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