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

Tomek Sowiński just at ask.me
Sat Nov 20 16:23:03 PST 2010


Tyro[a.c.edwards] <nospam at home.com> napisał(a):

> 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.

The harm is confusion. Now = on arrays always means aliasing, but with  
your proposal, it may *sometimes* mean dupping. Imagine real-life code  
with type aliasing and type inference in play, and trying to determine  
whether some line makes a dupping string<->char[] conversion or not.

What would be the harm if everyone just put .(i)dup where it belongs?

-- 
Tomek


More information about the Digitalmars-d mailing list