AA: How to lose a class
David
d at dav1d.de
Thu May 23 02:30:51 PDT 2013
http://dpaste.dzfl.pl/9172bed3
If mutable keys are allowed in AAs, then why don't they work and why
isn't that documentated? The docs imply this should work, but it
obviously doesn't.
Code:
import std.stdio;
@trusted nothrow void printHash(hash_t h) {
try { writefln("Called hashing func: %s", h); } catch(Exception e) {}
}
class Key{
hash_t hash;
this(size_t hash) {
this.hash = hash;
}
override hash_t toHash() const {
printHash(this.hash);
return hash;
}
override bool opEquals(Object o) const {
Key k = cast(Key)o;
if(k is null) {
return false;
}
return hash == k.hash;
}
override int opCmp(Object o) const {
Key k = cast(Key)o;
if(k is null) {
return -1;
}
if(k.hash < hash) {
return -1;
} else if(k.hash == hash) {
return 0;
} else {
return 1;
}
}
}
void main() {
int[Key] aa;
Key k1 = new Key(10);
aa[k1] = 3;
writeln(aa[new Key(10)]);
k1.hash = 5;
writeln((new Key(5)) in aa);
writeln((new Key(10)) in aa);
writeln(k1 in aa);
}
More information about the Digitalmars-d
mailing list