[Issue 6214] Don't influence foreach iteration on range
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Nov 2 21:12:39 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6214
Kenji Hara <k.hara.pg at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |DUPLICATE
--- Comment #2 from Kenji Hara <k.hara.pg at gmail.com> 2012-11-02 21:12:30 PDT ---
(In reply to comment #0)
> import std.stdio;
> void main() {
> foreach (i; 0 .. 10) {
> i += 1;
> write(i, " ");
> }
> }
>
> The output:
> 1 3 5 7 9
>
> In my opinion this is a bit bug-prone because in real code there is some risk
> of modifying the iteration variable "i" by mistake. (Note: here I am not
> talking about D for() loops. They are OK, their semantics is transparent
> enough. foreach() loops are less transparent and they *look* higher level than
> for() loops). I'd like the iteration variable to act as being a copy of the
> true loop variable as in Python. If this is a bit bad for foreach performance,
> then I'd like the compiler to forbid the mutation of the foreach iteration
> variable inside the foreach body.
This is now progressing by fixing bug 6652. So I'll mark this as a dup of it.
> Currently you can't solve the problem adding a const(int) to the iteration
> variable:
>
> import std.stdio;
> void main() {
> foreach (const(int) i; 0 .. 10) { // line 3
> write(i, " ");
> }
> }
>
> DMD 2.053 gives:
> test.d(3): Error: variable test.main.i cannot modify const
This is a dup of bug 4090, and it is mostly fixed in 2.061head.
*** This issue has been marked as a duplicate of issue 6652 ***
--
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