1

それぞれがいくつかの構造体を含む 2 つの連想配列を比較して、それらが互いに等しいかどうかを確認しようとしていますが、エラーが発生し、その理由がわかりません。構造体に関数がある場合に発生し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 を使用しています。

なぜこれが起こっているのか、意図した動作なのかバグなのか、どうすればよいのか、誰にもわかりませんか?

4

1 に答える 1

4

opEquals を const および non-ref に変更してみてください。

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

最近、dmd が変更され、opEquals シグネチャがより厳密になったと思います。

于 2013-01-01T23:44:36.467 に答える