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

Tyro[a.c.edwards] nospam at home.com
Sat Nov 20 14:52:12 PST 2010


On 11/20/2010 11:22 PM, Tomek Sowiński wrote:
> Dnia 20-11-2010 o 13:33:29 spir <denis.spir at gmail.com> napisał(a):
>
>> 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?
>
> 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?

What would be the harm if upon seeing your code the compiler did this:

char[] chars = "abc".dup;
char[] backdoor = chars;
string s = chars.idup;
assert (s == "abc");
backdoor.front = 'k';	// [1]
assert (s == "abc");

Slightly magical but works according to expectation. I don't see the 
problem.

[1] Assuming your are using std.array, this would fail because 
backdoor.front is not an lvalue.

>
>> 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
>
> Meh, immutable strings make life easier.
>



More information about the Digitalmars-d mailing list