ok, thanks.  And just a reminder that there is nothing about this on the errata.<br><br><br><h2><span class="mw-headline" id="Errata_for_.22The_D_Programming_Language.22_book"></span></h2><br><div class="gmail_quote">On Fri, Dec 24, 2010 at 11:51 AM, Andrei Alexandrescu <span dir="ltr"><<a href="mailto:SeeWebsiteForEmail@erdani.org">SeeWebsiteForEmail@erdani.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">On 12/24/2010 11:35 AM, Caligo wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I've been following the examples in the book and on page 8 we have this:<br>
<br>
import std.stdio;<br>
import std.string;<br>
<br>
void main(){<br>
<br>
      size_t[string] dictionary;<br>
     foreach(line; stdin.byLine()){<br>
               foreach(word; splitter(strip(line))){<br>
                           if(word in dictionary) continue;<br>
                     auto newID = dictionary.length;<br>
                           dictionary[word] = newID;<br>
                     writeln(newID, '\t', word);<br>
             }<br>
     }<br>
}<br>
<br>
With the latest GDC, which I think uses the latest 2.051, I get this error:<br>
dictionary.d:12: Error: associative arrays can only be assigned values<br>
with immutable keys, not char[]<br>
<br>
Someone told me on digitalmars-d.learn that it works with DMD, but I<br>
just downloaded the latest DMD that was just released and I still get<br>
the same error.<br>
<br>
Is there a workaround to this? and why the error?<br>
</blockquote>
<br></div></div>
Yah, this has been a long-standing issue. dmd has accepted for a while the code but produced incorrect results. Since then the bug has been fixed to refuse compilation.<br>
<br>
The problem is, line has type char[] and the map's key type is string, i.s. array of immutable char. To convert x to string you need to say either x.idup or to!string(x). In theory the compiler/library would be clever enough to do that automatically when necessary, but we decided against it for now.<br>

<br>
So: replace dictionary[word] with either dictionary[word.idup] or dictionary[to!string(word)]. A future printing will correct this mistake.<br><font color="#888888">
<br>
<br>
Andrei<br>
</font></blockquote></div><br>