[Issue 14984] Make it illegal (or at least a warning) to modify the iteration variable in foreach

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Sep 1 09:34:30 PDT 2015


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

Denis Shelomovskij <verylonglogin.reg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg at gmail.com

--- Comment #4 from Denis Shelomovskij <verylonglogin.reg at gmail.com> ---
(In reply to Marc Schütz from comment #3)
> @Kenji Hara:
> 
> Yes, I know that. My point is that it's too easy to forget to add `const`.

It's not a special `foreach` issue. It's a mistake to modify a variable in most
cases as often it is just a constant view of data so if one doesn't follow
"everything must be marked `const` except it is definitely mutable" it's his
coding style problem. Unfortunately D doesn't solve this issue as it doesn't
assume `const` by default. I hope the next D version will do it.

--


More information about the Digitalmars-d-bugs mailing list