Re: reddit.com: first Chapter of TDPL available for free
On Sat, Aug 8, 2009 at 8:44 PM, Jos van Udenj...@nospam.nl wrote: Andrei Alexandrescu wrote: http://www.reddit.com/r/programming/comments/975ng/diving_into_the_d_programming_language_tdpl/ (Don't tell anyone, but I plan to rewrite it.) Andrei Is this supposed to compile? I keep getting error messages. import std.stdio, std.string; void main() { uint[string] dic; foreach (line; stdin.byLine) { // Break sentence into words string[] words = split(strip(line)); // Add each word in the sentence to the vocabulary foreach (word; words) { if (word in dic) continue; // nothing to do uint newID = dic.length; dic[word] = newID; writeln(newID, '\t', word); } } } test.d(7): Error: function std.string.split (immutable(char)[] s) does not match parameter types (char[]) test.d(7): Error: cannot implicitly convert expression (strip(line)) of type char[] to immutable(char)[] test.d(7): Error: expected 2 function arguments, not 1 I've changed the code to: import std.stdio; import std.string; void main() { uint[string] dic; foreach (line; stdin.byLine) { string[] words = split(strip!(string)(line)); foreach (word; words) { if (word in dic) { continue; } uint newID = dic.length; dic[word] = newID; writeln(newID, '\t', word); } } } but I still get an error... test.d(12): Error: cannot implicitly convert expression (line) of type char[] to immutable(char)[] It's not documented, but the .byLine method returns char[], not immutable(char)[] (string). This is because the 'line' variable is reused on each new line of the input, to improve speed. I think to solved this, you should use: auto words = split(strip(line.idup)); The .idup creates a new duplicate of the line that is immutable. Now, why split() doesn't take a const(char)[] is beyond me..
Re: reddit.com: first Chapter of TDPL available for free
Jarrett Billingsley wrote: On Sat, Aug 8, 2009 at 8:44 PM, Jos van Udenj...@nospam.nl wrote: Andrei Alexandrescu wrote: http://www.reddit.com/r/programming/comments/975ng/diving_into_the_d_programming_language_tdpl/ (Don't tell anyone, but I plan to rewrite it.) Andrei Is this supposed to compile? I keep getting error messages. import std.stdio, std.string; void main() { uint[string] dic; foreach (line; stdin.byLine) { // Break sentence into words string[] words = split(strip(line)); // Add each word in the sentence to the vocabulary foreach (word; words) { if (word in dic) continue; // nothing to do uint newID = dic.length; dic[word] = newID; writeln(newID, '\t', word); } } } test.d(7): Error: function std.string.split (immutable(char)[] s) does not match parameter types (char[]) test.d(7): Error: cannot implicitly convert expression (strip(line)) of type char[] to immutable(char)[] test.d(7): Error: expected 2 function arguments, not 1 I've changed the code to: import std.stdio; import std.string; void main() { uint[string] dic; foreach (line; stdin.byLine) { string[] words = split(strip!(string)(line)); foreach (word; words) { if (word in dic) { continue; } uint newID = dic.length; dic[word] = newID; writeln(newID, '\t', word); } } } but I still get an error... test.d(12): Error: cannot implicitly convert expression (line) of type char[] to immutable(char)[] It's not documented, but the .byLine method returns char[], not immutable(char)[] (string). This is because the 'line' variable is reused on each new line of the input, to improve speed. I think to solved this, you should use: auto words = split(strip(line.idup)); The .idup creates a new duplicate of the line that is immutable. Now, why split() doesn't take a const(char)[] is beyond me.. Yep, that solves it, in both cases. Jos