Strange behavior of opEquals for structs
harfel
dadapapa at gmail.com
Wed Jun 19 15:28:55 UTC 2019
I am trying to overload opEquals for a struct. The struct will
hold class objects that define their own opEquals so the default
bitwise comparison is not good for me.
Everything works nicely if I compare the structs directly. Yet
when they are used as keys in an associative array, the code
throws an exception that I do not understand.
My minimal code example is this:
debug import std.stdio;
struct Foo(T) {
int[T] content;
bool opEquals(const(Foo!T) that) {
debug writeln("opEquals called");
return true;
}
alias content this;
}
class Bar { }
void main()
{
Foo!Bar a = Foo!Bar();
Foo!Bar b = Foo!Bar();
assert(a == b);
debug writeln("This works");
Foo!Bar[int] x = [1: Foo!Bar()];
x[1][new Bar] = 1;
Foo!Bar[int] y = [1: Foo!Bar()];
y[1][new Bar] = 1;
assert(x == y);
debug writeln("This does not work");
}
Here is what I get (using DMD64 D Compiler v2.086.0):
opEquals called
This works
object.Error@(0): TypeInfo.equals is not implemented
----------------
??:? bool object._xopEquals(const(void*), const(void*)) [0x49e844]
??:? const pure nothrow @trusted bool
object.TypeInfo_Struct.equals(const(void*), const(void*))
[0x49dcdb]
??:? _aaEqual [0x4a850b]
source/app.d:29 _Dmain [0x46c52f]
Program exited with code 1
Strange thing is that everything works nicely (but produces the
expected AssertionError) if I comment out Foo.opEquals. What is
the error message telling me and how can I fix it?
Thanks!!
More information about the Digitalmars-d-learn
mailing list