www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TDPL dictionary example - ERROR with dmd and gdc

reply Caligo <iteronvexor gmail.com> writes:
--001636c9262771879804982b6889
Content-Type: text/plain; charset=ISO-8859-1

I've been following the examples in the book and on page 8 we have this:

import std.stdio;
import std.string;

void main(){

     size_t[string] dictionary;
    foreach(line; stdin.byLine()){
              foreach(word; splitter(strip(line))){
                          if(word in dictionary) continue;
                    auto newID = dictionary.length;
                          dictionary[word] = newID;
                    writeln(newID, '\t', word);
            }
    }
}

With the latest GDC, which I think uses the latest 2.051, I get this error:
dictionary.d:12: Error: associative arrays can only be assigned values with
immutable keys, not char[]

Someone told me on digitalmars-d.learn that it works with DMD, but I just
downloaded the latest DMD that was just released and I still get the same
error.

Is there a workaround to this? and why the error?

--001636c9262771879804982b6889
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I&#39;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=
=A0=A0=A0=A0 size_t[string] dictionary;<br>=A0=A0=A0 foreach(line; stdin.b=

=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0 foreach(word; splitter(strip(line))){<b= r>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if(word= in dictionary) continue;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0= =A0 auto newID =3D dictionary.length;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0= =A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 dictionary[word] =3D newID;<br> =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 writeln(newID, &#39;\t&#3= 9;, word);<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }<br>=A0=A0=A0 }<br>}<br><br>Wi= th the latest GDC, which I think uses the latest 2.051, I get this error:<b= r>dictionary.d:12: Error: associative arrays can only be assigned values wi= th immutable keys, not char[]<br> <br>Someone told me on digitalmars-d.learn that it works with DMD, but I ju= st downloaded the latest DMD that was just released and I still get the sam= e error.=A0 <br><br>Is there a workaround to this? and why the error?<br> --001636c9262771879804982b6889--
Dec 24 2010
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 12/24/2010 11:35 AM, Caligo wrote:
 I've been following the examples in the book and on page 8 we have this:

 import std.stdio;
 import std.string;

 void main(){

       size_t[string] dictionary;
      foreach(line; stdin.byLine()){
                foreach(word; splitter(strip(line))){
                            if(word in dictionary) continue;
                      auto newID = dictionary.length;
                            dictionary[word] = newID;
                      writeln(newID, '\t', word);
              }
      }
 }

 With the latest GDC, which I think uses the latest 2.051, I get this error:
 dictionary.d:12: Error: associative arrays can only be assigned values
 with immutable keys, not char[]

 Someone told me on digitalmars-d.learn that it works with DMD, but I
 just downloaded the latest DMD that was just released and I still get
 the same error.

 Is there a workaround to this? and why the error?

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. 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. So: replace dictionary[word] with either dictionary[word.idup] or dictionary[to!string(word)]. A future printing will correct this mistake. Andrei
Dec 24 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 12/24/2010 12:35 PM, Caligo wrote:
 ok, thanks.  And just a reminder that there is nothing about this on the
 errata.

Fixed: http://www.erdani.com/tdpl/errata/ Thanks! Andrei
Dec 24 2010
prev sibling parent Caligo <iteronvexor gmail.com> writes:
--001636ef046900791f04982c4158
Content-Type: text/plain; charset=ISO-8859-1

ok, thanks.  And just a reminder that there is nothing about this on the
errata.



On Fri, Dec 24, 2010 at 11:51 AM, Andrei Alexandrescu <
SeeWebsiteForEmail erdani.org> wrote:

 On 12/24/2010 11:35 AM, Caligo wrote:

 I've been following the examples in the book and on page 8 we have this:

 import std.stdio;
 import std.string;

 void main(){

      size_t[string] dictionary;
     foreach(line; stdin.byLine()){
               foreach(word; splitter(strip(line))){
                           if(word in dictionary) continue;
                     auto newID = dictionary.length;
                           dictionary[word] = newID;
                     writeln(newID, '\t', word);
             }
     }
 }

 With the latest GDC, which I think uses the latest 2.051, I get this
 error:
 dictionary.d:12: Error: associative arrays can only be assigned values
 with immutable keys, not char[]

 Someone told me on digitalmars-d.learn that it works with DMD, but I
 just downloaded the latest DMD that was just released and I still get
 the same error.

 Is there a workaround to this? and why the error?

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. 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. So: replace dictionary[word] with either dictionary[word.idup] or dictionary[to!string(word)]. A future printing will correct this mistake. Andrei

--001636ef046900791f04982c4158 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable ok, thanks.=A0 And just a reminder that there is nothing about this on the = errata.<br><br><br><h2><span class=3D"mw-headline" id=3D"Errata_for_.22The_= D_Programming_Language.22_book"></span></h2><br><div class=3D"gmail_quote">= On Fri, Dec 24, 2010 at 11:51 AM, Andrei Alexandrescu <span dir=3D"ltr">&lt= ;<a href=3D"mailto:SeeWebsiteForEmail erdani.org">SeeWebsiteForEmail erdani= .org</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><= div class=3D"h5">On 12/24/2010 11:35 AM, Caligo wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> I&#39;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> =A0 =A0 =A0size_t[string] dictionary;<br> =A0 =A0 foreach(line; stdin.byLine()){<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 foreach(word; splitter(strip(line))){<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if(word in dictionary)= continue;<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 auto newID =3D dictionary.length;<= br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 dictionary[word] =3D n= ewID;<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 writeln(newID, &#39;\t&#39;, word)= ;<br> =A0 =A0 =A0 =A0 =A0 =A0 }<br> =A0 =A0 }<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 r= efuse compilation.<br> <br> The problem is, line has type char[] and the map&#39;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 eno= ugh 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 dictionar= y[to!string(word)]. A future printing will correct this mistake.<br><font c= olor=3D"#888888"> <br> <br> Andrei<br> </font></blockquote></div><br> --001636ef046900791f04982c4158--
Dec 24 2010