Bad AA behavior
Dave
Dave_member at pathlink.com
Sat Dec 23 20:27:39 PST 2006
BCS wrote:
> void main()
> {
> byte[char[]] set;
> char[] it = "it".dup;
> set[it] = 0;
> assert("it" in set); // pass
> it[0] = 'a';
> it[1] = 'a';
> assert("it" in set); // fail
> }
>
>
> This can't be correct.
>
I believe it is correct because of the distinction between value and reference types made throughout
D. I don't find the behavior inconsistent, although I've been bitten by it once or twice so I'd
agree that it's not always intuitive.
byte[char[]] set; // is an array of bytes indexed by char[]'s (a reference type)
//set[it] = 0;
set[it.dup] = 0; // this (obviously) will do what you expect
To get the behavior you suggest (and be consistent), then UDT's would all have to have a copy ctor,
either explicit or implicit. If implicit, then how is the compiler supposed to do a deep copy on
objects defined like:
class C
{
char* ptr;
//...
}
?
If manual memory management for D was the norm, then I'd say you have a much stronger argument. But
since GC is the norm. (and in part because GC is the norm D also does away with copy ctor's) the
current behavior is correct, IMHO.
- Dave
More information about the Digitalmars-d-bugs
mailing list