Thread overview
[Issue 14984] Make it illegal (or at least a warning) to modify the iteration variable in foreach
Aug 30, 2015
yebblies
Aug 30, 2015
Kenji Hara
Aug 30, 2015
Marc Schütz
Sep 01, 2015
Denis Shelomovskij
Sep 01, 2015
Denis Shelomovskij
Mar 08, 2016
ryan@rcorre.net
Oct 10, 2022
RazvanN
August 30, 2015
https://issues.dlang.org/show_bug.cgi?id=14984

yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com

--- Comment #1 from yebblies <yebblies@gmail.com> ---
https://issues.dlang.org/show_bug.cgi?id=6652

--
August 30, 2015
https://issues.dlang.org/show_bug.cgi?id=14984

--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> ---
Currently it's intentional. When you declare a index variable in foreach statement, it works like an auto variable scoped in the foreach body.

If you need non-mutable index variable, you can write code like:

foreach (const i, v; arr) { ... }
foreach (const i; 0..len) { ... }

--
August 30, 2015
https://issues.dlang.org/show_bug.cgi?id=14984

--- Comment #3 from Marc Schütz <schuetzm@gmx.net> ---
@Kenji Hara:

Yes, I know that. My point is that it's too easy to forget to add `const`.

--
September 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14984

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

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

--- Comment #4 from Denis Shelomovskij <verylonglogin.reg@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.

--
September 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14984

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--
March 08, 2016
https://issues.dlang.org/show_bug.cgi?id=14984

ryan@rcorre.net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ryan@rcorre.net

--
May 07, 2016
https://issues.dlang.org/show_bug.cgi?id=14984

greensunny12@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |greensunny12@gmail.com

--
October 10, 2022
https://issues.dlang.org/show_bug.cgi?id=14984

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305@gmail.com
         Resolution|---                         |WORKSFORME

--- Comment #5 from RazvanN <razvan.nitu1305@gmail.com> ---
Right now, if we modify the element of a foreach range, there is no problem because we are operating on a copy of the original element. If we need to modify the actual element, then we need to add ref. That stands true for the iteration variable also. I don't see why we would deprecate any of this behavior because it offers maximum flexibility.

--