[Issue 9087] Value modified in foreach warning

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 28 15:09:03 PST 2012


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



--- Comment #9 from Jonathan M Davis <jmdavisProg at gmx.com> 2012-11-28 15:08:58 PST ---
> I think you are missing something. If you use "ref" or "const ref" no copy is
> performed:

If don't want a copy, then use ref. If you want a copy, then don't use ref.
Disallowing mutating the loop variable if not using ref would be annoying. It
would force you to manually copy the loop variable if you wanted to mutate it.
If we were doing that, we might as well make the loop variable always ref. I
think that that's a horrible idea. And warning about something is as good as
disallowing, because it's an error for anyone compiling with -w and it's
terrible practice to leave any warnings in your code anyway.

I understand that there may be bugs caused by failing to use ref when you meant
to, but you're basically proposing that a foreach loop always use ref and that
anyone wanting it to not be ref must copy the loop variable themselves. Not to
mention, in some cases, you _can't_ use ref (e.g. if iterating over a range,
and its front doesn't return by ref), which would mean that you'd have to make
a useless copy of a non-ref variable in that case. So, I think that a warning
like you propose is completely untenable.

-- 
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