[Issue 20901] static foreach must deep-copy front() per iteration

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 10 13:29:34 UTC 2020


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

Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86                         |All
            Summary|arrays confuse static       |static foreach must
                   |foreach                     |deep-copy front() per
                   |                            |iteration
                 OS|Windows                     |All

--- Comment #4 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
> Everything that is not a deep copy is plainly wrong.

It seems you're right. I though this code would cause problems, but it's
statically disallowed:

static foreach (e; S()) {
    // Change an element that will also be present in the next iteration.
    e[0]++; // Error: cannot modify constant 2
    writeln(e);
}

If the above would compile, it'd be a clear issue. However, it doesn't, so it
seems a deep copy should indeed work.

--


More information about the Digitalmars-d-bugs mailing list