Summary: String assignment in foreach loop breaks immutability
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Andrej Mitrovic <> 2010-10-15 
12:04:00 PDT ---
Since this example uses the Registry, you need Windows to compile it:

module mymodule;

import std.stdio : writeln, write;

void main()
   Key HKLM = Registry.localMachine;
   Key SFW = HKLM.getKey("software");

   string[] names;

   foreach (Key key; SFW.keys())
       string name =;
       // string name =; // workaround for the issue
       names ~= name;

   foreach (name; names)
       write(name, ", ");

The resulting string array has strings that are overwritten with each other,
and in my case the results are similar to this:

Sun Microsystems, Sun Micros, Sun , Sun Micr, Sun, Sun Mic,...

And it goes like that for a hundred or so values, then switches to the next
registry key name and writes more overwwritten strings like that.

The commented out code is the workaround, however string to string assignment
should be safe without a need for .idup.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to