Search code examples
d

object.Error when comparing associative arrays containing structs in D


I am trying to compare two associative arrays, each containing some structs, to see if they are equal to each other, but I am getting an error and I don't know why. It happens if the structs have an opEquals function. Comparing traditional (non-associative) arrays with the same kind of structs works fine. Here is a simple program that illustrates the problem:

#! /usr/bin/rdmd
import std.stdio;

void main() {
    // outputs "true"
    writeln([Thing()] == [Thing()]);

    // throws an error
    writeln(["foo": Thing()] == ["foo": Thing()]);
}

struct Thing {
    /* This function needs to be defined to trigger the error but it doesn't
       seem to matter what I put in it. */
    bool opEquals(ref Thing other) {
        return true;
    }
}

Here is the error message:

object.Error: TypeInfo.equals is not implemented
----------------
5   structAaTest                        0x0000000100003b20 const(pure nothrow @trusted bool function(const(void*), const(void*))) object.TypeInfo_Struct.equals + 64
6   structAaTest                        0x0000000100013a70 extern (C) int rt.aaA._aaEqual(TypeInfo, rt.aaA.AA, rt.aaA.AA).int _aaKeys_x(rt.aaA.aaA*) + 152
7   structAaTest                        0x00000001000139ad _aaEqual + 261
8   structAaTest                        0x0000000100000fbb _Dmain + 319
9   structAaTest                        0x00000001000151de extern (C) int rt.dmain2.main(int, char**).void runMain() + 34
10  structAaTest                        0x0000000100014b95 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 45
11  structAaTest                        0x0000000100015228 extern (C) int rt.dmain2.main(int, char**).void runAll() + 56
12  structAaTest                        0x0000000100014b95 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 45
13  structAaTest                        0x0000000100014b1f main + 235
14  structAaTest                        0x0000000100000e74 start + 52
15  ???                                 0x0000000000000001 0x0 + 1
----------------

I am using DMD v2.060 and OS X 10.6.8.

Does anyone know why this is happening, whether it is intended behavior or a bug, and what to do about it?


Solution

  • Try changing your opEquals to be const and non-ref.

    bool opEquals(Thing other) const {
         return true;
    }
    

    I think dmd recently changed to be a lot stricter about the opEquals signature.