constness of AA keys (Was: Re: Need help locating AA bug)
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Feb 27 20:06:30 PST 2012
On Mon, Feb 27, 2012 at 09:34:11PM -0500, Jonathan M Davis wrote:
[...]
> The keys to AA's are supposed to be _immutable_. Really, this should
> be enforced by the compiler (and it is on some level, but not to the
> point that code like int[int[]] is illegal like it should be). The
> fact that the implementation is using const internally just makes it
> that much worse.
[...]
Does it make sense to make T in S[T] implicitly immutable? Otherwise
it's quite a handful to keep typing S[immutable(T)].
In any case, something is horribly broken somewhere, because even if you
had immutable keys, you still hit the same bug:
int[dstring] map1;
map1 = ["abc"d: 1];
assert(map1["abc"d]==1); // assertion fails!
Whereas if you wrote:
int[dstring] map2;
map2["abc"d] = 1;
assert(map2["abc"d]==1); // OK!
Passing the AA's to my debugging code reveals that we're running into
the same problem: map1 has its internal key type mapped to
immutable(dchar)[] (i.e., dstring, as it should be), but map2 has its
internal key type mapped to dchar[], causing hash values to be screwed
up.
It seems that AA.get() and AA[...] both somehow lose const/immutable
qualifiers. You still get the same problem even if you explicitly create
a dstring:
int[dstring] map1 = ["abc"d: 1];
dstring dkey = "abc"d;
assert(map1[dkey]==1); // assertion fails!
Something is *very* broken somewhere in there. :-(
T
--
MASM = Mana Ada Sistem, Man!
More information about the Digitalmars-d
mailing list