http://d.puremagic.com/issues/show_bug.cgi?id=4290
Summary: 'Fragile' opCmp/toHash signature errors Product: D Version: future Platform: All OS/Version: All Status: NEW Keywords: diagnostic Severity: enhancement Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: bearophile_h...@eml.cc --- Comment #0 from bearophile_h...@eml.cc 2010-06-06 13:08:45 PDT --- This is a reduced version of a bug in my code. This D2 code compiles, but at the end there are two instances of Foo inside the AA. struct Foo { int x; const bool opEquals(ref const Foo f) { return true; } int opCmp(ref const Foo f) { return 0; } hash_t toHash() { return 10; } } void main() { int[Foo] aa; aa[Foo(1)]++; aa[Foo(2)]++; assert(aa.length == 1); // asserts, it's equal to 2 } The problem is caused just by two missing 'const', the following code is correct: struct Foo { int x; const bool opEquals(ref const Foo f) { return true; } int opCmp(ref const Foo f) const { return 0; } hash_t toHash() const { return 10; } } void main() { int[Foo] aa; aa[Foo(1)]++; aa[Foo(2)]++; assert(aa.length == 1); // OK } In my opinion the D2 has to catch such programming errors and refuse opCmp and toHash with a wrong signature, to avoid similar common bugs. See also bug 3844 ----------------------------- An alternative solution is to invent something similar to the 'override' keyword for struct member functions: struct Foo { int x; const bool opEquals(ref const Foo f) { return true; } static_override int opCmp(ref const Foo f) { // error, doesn't override the default opCmp return 0; } static_override hash_t toHash() { // error, doesn't override the default toHash return 10; } } void main() { int[Foo] aa; aa[Foo(1)]++; aa[Foo(2)]++; assert(aa.length == 1); // asserts, it's equal to 2 } But D2 structs don't support inheritance, so static_override is of limited usefulness, it looks over-engineering. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------