[Issue 5059] String assignment in foreach loop breaks immutability

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 24 15:16:51 PDT 2011


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



--- Comment #4 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2011-05-24 15:12:35 PDT ---
Change what exactly to const(char[])? You probably mean my "name" variable in
the example code.

I think this is again that issue of implicitly reusing a variable in a foreach
loop. I really wish this was more explicit instead of relying on documentation,
e.g. I'd love it if it were like this:

foreach (ref buffer; iterator()) { }  // it's reusing buffer on each iteration
foreach (buffer; iterator()) { }      // allocates new buffer on each iteration

Maybe it's easy to reason about the current foreach mechanism when you only
have one foreach loop. But when you have multiple foreach statements
intertwined with dozens of different lines it's easy to miss that you should
have duped something. 

It took me a good while before I realized what was wrong with my code until I
ran it down to a simple test case like in the original post. I simply thought
"hey, I'm assigning an immutable string to an immutable string, what could go
wrong?".

But this is more worthy of a post in the NG then to discuss it in here I
guess..

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