Re: [Issue 6652] New: foreach parameter with number range is always ref

2012-06-21 Thread IK

foreach parameter with number range is always ref
I noticed this by getting into infinite loops. But that, more 
than anything, hints of very bad programming style. :)




[Issue 6652] New: foreach parameter with number range is always ref

2011-09-12 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=6652

   Summary: foreach parameter with number range is always ref
   Product: D
   Version: D2
  Platform: Other
OS/Version: FreeBSD
Status: NEW
  Severity: normal
  Priority: P2
 Component: DMD
AssignedTo: nob...@puremagic.com
ReportedBy: d...@dawgfoto.de


--- Comment #0 from d...@dawgfoto.de 2011-09-12 10:19:15 PDT ---
void main() {
  size_t cnt;
  foreach(ulong n; 0 .. 10)
  {
++n;
++cnt;
  }
  assert(cnt == 10);

  cnt = 0;
  foreach(ref ulong n; 0 .. 10)
  {
++n;
++cnt;
  }
  assert(cnt == 5);
}

---

As this is rewritten in terms of a for loop all writes to n will
alter the loop.
A hidden copy of n is needed for non-ref parameters to match the range
foreach semantic.

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