[Issue 9087] Value modified in foreach warning

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 28 05:14:12 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=9087



--- Comment #5 from bearophile_hugs at eml.cc 2012-11-28 05:14:12 PST ---
(In reply to comment #3)

> Another is to keep language expressive with a little more user's learning.

1) Learning to avoid bugs is a very painful process, it takes lot of time, and
some bugs escape even the most precise programmers. Experience shows that
automatic tools (like the compiler) are needed.
2) Modifying a copy of a struct/value in a foreach is a rare operation. And
there is no significant decrease in D expressiveness, because there are other
ways to do it:

// program#3
struct S { int x; }
void main() {
    auto items = [S(1), S(2), S(3)];
    foreach (it; items) {
        auto itc = it;
        itc.x++;
    }
    for (size_t i = 0; i < items.length; i++) {
        auto it = items[i];
        it.x++;
    }
}

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


More information about the Digitalmars-d-bugs mailing list