[Issue 12109] New: foreach with class alias this range iteration inconsistency
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Feb 8 10:28:36 PST 2014
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 at puremagic.com
ReportedBy: peter.alexander.au at gmail.com
--- Comment #0 from Peter Alexander <peter.alexander.au at gmail.com> 2014-02-08 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: -------
More information about the Digitalmars-d-bugs
mailing list