[Issue 2043] Closure outer variables in nested blocks are not allocated/instantiated correctly: should have multiple instances but only have one.

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Oct 2 10:31:48 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=2043

Ketmar Dark <ketmar at ketmar.no-ip.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ketmar at ketmar.no-ip.org

--- Comment #17 from Ketmar Dark <ketmar at ketmar.no-ip.org> ---
(In reply to entheh from comment #16)
i believe that modifying `i` should be allowed here. but without 'ref' `i` is a
copy of internal counter, so your sample correctly iterates 10 times, updating
`i` to correct value on each iteration. but this works (and it's correct too):

  foreach (ref i; 0..10) {
    ++i; // skip one
    writeln(i);
  }

this outputs 1, 3, 5, 7, 9.

> If 'i' became a new variable for each iteration, then the modify-assign
> would not have the effect intended.
it works this way now. ;-)

--


More information about the Digitalmars-d-bugs mailing list