我正在尝试比较两个关联数组,每个数组都包含一些结构,以查看它们是否彼此相等,但出现错误,我不知道为什么。如果结构具有opEquals
功能,就会发生这种情况。将传统(非关联)数组与相同类型的结构进行比较可以正常工作。这是一个说明问题的简单程序:
#! /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;
}
}
这是错误消息:
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
----------------
我正在使用 DMD v2.060 和 OS X 10.6.8。
有谁知道为什么会发生这种情况,无论是预期的行为还是错误,以及如何处理?