When are associative arrays meant to throw a RangeError?

Daniel Murphy yebblies at nospamgmail.com
Sat Feb 18 05:22:34 PST 2012


"Ben Davis" <entheh at cantab.net> wrote in message 
news:jho2mf$2a1t$1 at digitalmars.com...
> Starting with magic initialisation then...
>

I meant a different magic initialization:
int[int] aa = null;
aa[3] = 7; // aa is magically not null any more

> Is it vital that e[nonexistentKey] throw a RangeError, or could it just 
> always return the type's default value if the key is absent?
>
This is what it does.

> If you change that, then you can make assignment evaluate the RHS fully 
> before even creating the LHS entry, and you won't in the process break the 
> common case where people want to go
>
> count[key]++;
> or
> array[key]~=element;
>
> without worrying about whether it's the first time for that key or not.
>

This problem is just a bug in code generation from what I can tell, because 
lowering it manually results in the rhs being evaluated first.

import std.stdio;

int* getp()
{
    writeln("1");
    return new int;
}

void main()
{
    *getp() += { writeln("2"); return 1; }();
}

prints:
2
1

I have no idea where this is happening in the compiler. 




More information about the Digitalmars-d mailing list