On Wed, 16 May 2012 03:20:39 -0400, Jonathan M Davis <[email protected]> wrote:

On Wednesday, May 16, 2012 09:15:06 bearophile wrote:
Nick Sabalausky:
> It seems that foreach is iterating over an implicit copy of
> 'foo' instead of
> 'foo' itself. Is this correct behavior, or a DMD bug?

This program prints "[11, 21, 31]" so for fixed-size arrays (that
are values as much as structs) it doesn't perform a copy:


import std.stdio;
void main() {
      int[3] a = [10, 20, 30];
      foreach (ref x; a)
          x++;
      writeln(a);
}

It probably slices the array, which would mean that it was operating on a
dynamic array rather than a static one. But it could just generate different
code for static arrays.

foreach does *not* use range primitives for arrays, they are special.

-Steve

Reply via email to