[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 bearophile_h...@eml.cc changed: What|Removed |Added CC||bearophile_h...@eml.cc --- Comment #1 from bearophile_h...@eml.cc 2011-09-12 12:24:41 PDT --- Recently I have started a very long thread about this problem: http://www.digitalmars.com/d/archives/digitalmars/D/About_foreach_loops_138630.html http://www.digitalmars.com/d/archives/digitalmars/D/Re_About_foreach_loops_138666.html An alternative and probably a bit better solution is to think of 0..10 as a immutable entity (just like the integer number "1" is immutable, likewise a range of numbers is immutable), so the foreach index is a const (the compiler keeps only one index, for efficiency, and modifies this const index): foreach (int i; 0 .. 10) { i++; // forbidden, i is const } If you want to modify the index variable inside the loop, then you use a for() loop. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #2 from d...@dawgfoto.de 2011-09-12 14:35:51 PDT --- Making a const/immutable copy is not the right solution to this. Instead a mutable copy of a hidden loop variable should be made. Being a copy is the common behavior for non-ref foreach arguments, to my surprise it has even become my intuitive assumption of what's happening. The old behavior can be achieved through a ref argument. not possible using const: foreach(i; 1 .. 10) while(i--) { do some } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #3 from bearophile_h...@eml.cc 2011-09-12 15:10:08 PDT --- (In reply to comment #2) > Making a const/immutable copy is not the right solution to this. Keep in mind that foreach(i;0..10) must have *zero* abstraction penalty over a for loop even with non-optimizing D compilers, because it's meant to replace for loops everywhere possible. The more abstraction you put into foreach there higher the probability it will not have zero abstraction penalty (currently it has a bit of penalty for nested loops, sometimes). > not possible using const: > foreach(i; 1 .. 10) > while(i--) { do some } Use a for loop :-) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #4 from d...@dawgfoto.de 2011-09-13 00:02:29 PDT --- https://github.com/D-Programming-Language/dmd/pull/377 Foreach arguments behave like function arguments. Here they don't. The variable can be optimized out, if no altering happens. This will not happen in a debug build, where it is irrelevant in comparison to every variable being accessed through the stack. You can use ref, if you're having too expensive copies foreach(ref const i; iter(0) .. iter(10)) as with every other foreach argument. Most important it has an explicit rule, that one can alter the loop index through using a ref index. foreach(ref idx, v; [0, 1, 2, 3, 4, 5]) idx += stride - 1; -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #5 from bearophile_h...@eml.cc 2011-09-13 03:59:49 PDT --- (In reply to comment #4) > https://github.com/D-Programming-Language/dmd/pull/377 > - I've double checked that a simple size_t index is optimized out if unaltered I suggest you to check it fifteen more times, using 4 nested foreach, with some code inside the bodies, with other data types (ubyte, short, ulong, real, double, etc), etc. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #6 from d...@dawgfoto.de 2011-09-13 21:49:08 PDT --- It really is not that much an issue of performance. The compiler should be able to eliminate dead assignments and *& for ref parameters. The issue is that of breaking code. I don't know any feasible solution to attach a deprecation to this now and change it later. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Masahiro Nakagawa changed: What|Removed |Added CC||repeate...@gmail.com --- Comment #7 from Masahiro Nakagawa 2012-06-11 08:47:41 PDT --- I hit this issue today. Current behavior is different from foreach semantics. So, I agree dawg opinion. We should fix this bug! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Kenji Hara changed: What|Removed |Added Keywords||pull Platform|Other |All OS/Version|FreeBSD |All --- Comment #8 from Kenji Hara 2012-06-15 10:22:02 PDT --- I've opened three pulls: https://github.com/D-Programming-Language/dmd/pull/1008 https://github.com/D-Programming-Language/dmd/pull/1009 https://github.com/D-Programming-Language/dmd/pull/1010 To reduce breaking of existing codes, 1. Warn to modifying loop variable in foreach body. It is shown only when -w switch is specified. 2. Deprecate modifying loop variable in foreach body. If user specifies -d switch, it is allowed. 3. Allow modifying loop variable in foreach body, and it does not affect to the number of iterations of the loop. How about? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #9 from bearophile_h...@eml.cc 2012-06-15 11:56:35 PDT --- (In reply to comment #8) > To reduce breaking of existing codes, > 1. Warn to modifying loop variable in foreach body. >It is shown only when -w switch is specified. > 2. Deprecate modifying loop variable in foreach body. >If user specifies -d switch, it is allowed. > 3. Allow modifying loop variable in foreach body, and it does not affect to >the number of iterations of the loop. This is great, you are the best Kenji Hara. I prefer the number 2. I think it breaks none of my programs. The number 3 is a trap, because it silently changes the semantics of old D code. And it's bug-prone for new D programmers too because they can change the variable by mistake. Generally immutable variables are safer. Are you able and willing to compile the whole Phobos with the option number 2? So we can see how often Phobos code change the foreach-on-range iteration variable. What's the semantics of this, now? foreach (ref int i; 0 .. 10) i++; -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Kenji Hara changed: What|Removed |Added Keywords||wrong-code --- Comment #10 from Kenji Hara 2012-06-15 19:40:31 PDT --- (In reply to comment #9) > (In reply to comment #8) > > > To reduce breaking of existing codes, > > 1. Warn to modifying loop variable in foreach body. > >It is shown only when -w switch is specified. > > 2. Deprecate modifying loop variable in foreach body. > >If user specifies -d switch, it is allowed. > > 3. Allow modifying loop variable in foreach body, and it does not affect to > >the number of iterations of the loop. > > This is great, you are the best Kenji Hara. > > I prefer the number 2. I think it breaks none of my programs. They are the phases to change behavior. I think we should allow modifying loop variable in foreach body, but it should not affect to iteration. > The number 3 is a trap, because it silently changes the semantics of old D > code. And it's bug-prone for new D programmers too because they can change the > variable by mistake. Generally immutable variables are safer. #3 is a goal. > Are you able and willing to compile the whole Phobos with the option number 2? > So we can see how often Phobos code change the foreach-on-range iteration > variable. http://d.puremagic.com/test-results/pulls.ghtml See auto tester. With all pull request, Phobos compile succeeds. So there is no code that changes the foreach-on-range iteration variable. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Ryuichi OHORI changed: What|Removed |Added CC||r.97...@gmail.com --- Comment #11 from Ryuichi OHORI 2012-06-15 19:57:43 PDT --- (In reply to comment #9) > I prefer the number 2. I think it breaks none of my programs. > > The number 3 is a trap, because it silently changes the semantics of old D > code. And it's bug-prone for new D programmers too because they can change the > variable by mistake. Generally immutable variables are safer. In my point of view, as a newcomer to D, more bug-prone is the current behavior. Foreach statement provides iteration over arrays, ranges, etc, and notation of range "0..n" also *looks like* a collection. So, foreach range statements should work like foreach over collection. I have wrote to stuck in my program: foreach (i; 0..M^^n) { foreach (j; 0..n) { a[j] = i % M; i /= M; } // operations which use a but i } which I wrote in Python before: for i in range(M**n): for j in range(n): a[j] = i % M i /= M # operations which use a but i and was sad to see an infinite loop. Even a new programmer *intends* to change the value of i when changing, if not just a typo. If someone want to affect loop, s/he can write i = 0; while (i < 10) { // operations which change the value of i i += 1; } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #12 from bearophile_h...@eml.cc 2012-06-16 05:07:21 PDT --- (In reply to comment #10) > They are the phases to change behavior. I see. > I think we should allow modifying loop > variable in foreach body, but it should not affect to iteration. Generally changing the iteration variable isnt't a very good idea. It looks bug-prone, like modifying function arguments inside functions :-) foreach (i; 0 .. 10) { i++; writeln(i); } > So there is no > code that changes the foreach-on-range iteration variable. Good. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #13 from bearophile_h...@eml.cc 2012-06-16 05:14:39 PDT --- If phase 3 will be accepted, I hope this syntax too will be accepted: foreach (const i; 0 .. 10) {} -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #14 from bearophile_h...@eml.cc 2012-06-16 05:15:07 PDT --- (In reply to comment #11) > In my point of view, as a newcomer to D, more bug-prone is the current > behavior. Of course. But here the comparison wasn't between the current behavour and the phase 3. It was mostly a comparison between the phase 2 and phase 3. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #15 from Kenji Hara 2012-06-16 05:27:32 PDT --- (In reply to comment #13) > If phase 3 will be accepted, I hope this syntax too will be accepted: > > foreach (const i; 0 .. 10) {} I think it should be allowed. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #16 from Ryuichi OHORI 2012-06-16 09:00:32 PDT --- (In reply to comment #14) > > In my point of view, as a newcomer to D, more bug-prone is the current > > behavior. > Of course. But here the comparison wasn't between the current behavour and the > phase 3. It was mostly a comparison between the phase 2 and phase 3. What followed it was my opinion to it: > > Even a new programmer *intends* to change the value of i when changing, if > > not just a typo. The problem is that changing the value of a variable affects loop, not changing itself. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #17 from bearophile_h...@eml.cc 2012-06-17 15:31:45 PDT --- Just a note. void main() { import std.stdio; auto array = [10, 20, 30, 40, 50]; foreach (i, item; array) { writeln(item); i++; } } Currently it prints: 10 30 50 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #18 from d...@dawgfoto.de 2012-06-19 06:10:15 PDT --- >How about? Sounds great. It doesn't break code and allows us to fix this finally. >foreach (i, item; array) Yeah, it should apply to the index variable as well. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #19 from Kenji Hara 2012-06-21 08:32:51 PDT --- (In reply to comment #18) > >How about? > Sounds great. It doesn't break code and allows us to fix this finally. > > >foreach (i, item; array) > Yeah, it should apply to the index variable as well. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #20 from Kenji Hara 2012-06-21 08:35:11 PDT --- (In reply to comment #19) > (In reply to comment #18) > > >How about? > > Sounds great. It doesn't break code and allows us to fix this finally. > > > > >foreach (i, item; array) > > Yeah, it should apply to the index variable as well. (Sorry, I accidentally pressed the "commit" button...) OK. Now, my pull requests also care the key of array in iteration. You can see the test case: https://github.com/D-Programming-Language/dmd/pull/1008/files#L5L-1 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #21 from github-bugzi...@puremagic.com 2012-06-21 14:21:51 PDT --- Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/8ea1a0ef35b9cef3047699396487f4d2fe149a39 fix Issue 6652 - 1. Warn modifying non ref variable if -w is specified. https://github.com/D-Programming-Language/dmd/commit/38a0a5141a3455395e8b9571a57bf85ed698c6b3 Merge pull request #1008 from 9rnsr/fix6652_1 fix Issue 6652 - 1. Warn modifying non ref variable if -w is specified. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #22 from d...@dawgfoto.de 2012-06-22 07:37:40 PDT --- >Commits pushed to master Great. So what's the time frame until deprecation, 6 month? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #23 from Kenji Hara 2012-11-02 21:12:30 PDT --- *** Issue 6214 has been marked as a duplicate of this issue. *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #24 from bearophile_h...@eml.cc 2012-11-03 07:06:12 PDT --- See also: http://forum.dlang.org/thread/znbtczbgipqqzllaf...@forum.dlang.org -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #25 from github-bugzi...@puremagic.com 2012-11-13 14:38:59 PST --- Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/780ce35e8338435e5d063da2cc93360d7f0aecbd Appendix for Issue 6652 If *implicit ref* foreach key doesn't have a mutable type, warning is not necessary. https://github.com/D-Programming-Language/dmd/commit/882324ed350d087949e000a78b32c75396ea003a Merge pull request #1276 from 9rnsr/fix6652a Appendix for Issue 6652 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #26 from github-bugzi...@puremagic.com 2013-01-24 09:56:33 PST --- Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/ac7fd08a9e08fb333d9341f21bdb19ba42e1ee38 fix Issue 6652 - 2. Deprecate modifying non ref variable. https://github.com/D-Programming-Language/dmd/commit/ae95b0f935e00ec03224180a24c4396485367eb8 Merge pull request #1009 from 9rnsr/fix6652_2 fix Issue 6652 - 2. Deprecate modifying non ref variable. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Andrej Mitrovic changed: What|Removed |Added Status|NEW |RESOLVED CC||andrej.mitrov...@gmail.com Resolution||FIXED --- Comment #27 from Andrej Mitrovic 2013-02-05 13:16:06 PST --- This is now a deprecated feature. I've closed the report, but perhaps it should stay open until the feature is completely gone? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 --- Comment #28 from github-bugzi...@puremagic.com 2013-03-06 21:32:07 PST --- Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/6e018c83c2fcd138c6596ca63a3fbf9140e9c69c fix Issue 6652 - 3. Change modifying non ref variable allowed and not affect to loop https://github.com/D-Programming-Language/dmd/commit/ad74ad3bd8ae763db3f68c0a6647cb1909d477dd Merge pull request #1010 from 9rnsr/fix6652_3 fix Issue 6652 - 3. Change modifying non ref variable allowed and not affect to loop -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Walter Bright changed: What|Removed |Added CC||bugzi...@digitalmars.com Severity|normal |enhancement --- Comment #29 from Walter Bright 2013-03-06 21:36:00 PST --- This last pull is for #3. It's also an enhancement. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 6652] foreach parameter with number range is always ref
http://d.puremagic.com/issues/show_bug.cgi?id=6652 Martin Nowak changed: What|Removed |Added CC||la...@virginia.edu --- Comment #30 from Martin Nowak 2013-11-01 12:16:23 PDT --- *** Issue 2543 has been marked as a duplicate of this issue. *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---