Principled method of lookup-or-insert in associative arrays?

Tomek Sowiński just at ask.me
Sat Nov 20 12:21:58 PST 2010


Dnia 20-11-2010 o 17:09:00 spir <denis.spir at gmail.com> napisał(a):

>> It's busting the whole const system to smithereens.
>>
>> char[] chars = "abc";
>> char[] backdoor = chars;
>> string s = chars;
>> assert (s == "abc");
>> backdoor.front = 'k';
>> assert (s == "abc"); // fails. not so immutable, huh?
> ???
> backdoor and s do not denote the same element. One is a mutable array,  
> the other is immutable. Why should changing backdoor affect s? Whether  
> backdoor and chars denote the same array depends on whether "=" copies  
> or not dyn arrays. But from immutable string to mutable array, there  
> must be a copy (read: dup).

OK, I get it. I still like it the way it is. Having worked with C++ code  
with overloaded assignment ops and implicit conversions, I see = that  
sometimes dups and sometimes aliases as a source of confusion and  
performance bugs. All this just to remove .idup (it's 5 chars!).

-- 
Tomek


More information about the Digitalmars-d mailing list