On Sat, 20 Nov 2010 00:07:57 -0800 Andrei Alexandrescu <seewebsiteforem...@erdani.org> wrote:
> TDPL has an example that can be reduced as follows: > > void main() { > uint[string] map; > foreach (line; stdin.byLine()) { > ++map[line]; > } > } > > byLine reuses its buffer so it exposes it as char[]. Therefore, > attempting to use map[line] will fail. The program compiled and did the > wrong thing because of a bug. > > The challenge is devising a means to make the program compile and work > as expected. Looking up an uint[string] with a char[] should work, and > if the char[] is to be inserted, a copy should be made (via .idup or > to!string). The rule needs to be well defined and reasonably general. > > The effect is something like this: > > void main() { > uint[string] map; > foreach (line; stdin.byLine()) { > auto p = line in map; > if (p) ++*p; > else map[line.idup] = 1; > } > } > > Ideally the programmer would write the simple code (first snippet) and > achieve the semantics of the more complex code (second snippet). Any ideas? I find this proposal really necessary. But aren't there two issues here? * Comparison (for lookup) by value equality should not care about qualifiers (ie compare raw content, here plain array memory areas). * Assignment should perform "qualification conversion" automatically, eg char[] chars = "abc"; string s = chars; This involves no implicit magic here, as target qualification is explicit. So, why not? There is a repetitive programming pattern in D: * play around with *string's in general * as soon as text processing is needed, convert to *char[] * when finished, convert back to *string This should not be implicit, but automatic. Is there anything risky here? Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com