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

Tyro[a.c.edwards] nospam at home.com
Sat Nov 20 17:02:35 PST 2010


On 11/21/2010 9:23 AM, Tomek Sowiński wrote:
> 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.

alias char[] firstClassString;
alias firstClassString stringGold;
alias stringGold lightAtEndOfTunnel;
alias lightAtEndOfTunnel daBomb;
alias daBomb iAmThe;

iAmThe man;

if I want to know the actual type of shiznit:

writeln(typeid(man)); // outputs char[]

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

Actually, I don't see any harm in using .(i)dup. However, my limited 
experience does not allow me to think on the same level as you so I'm 
just trying to understand your point a little better. That being said, I 
still don't understand the gravity of the implied problem.


More information about the Digitalmars-d mailing list