看看我正在构建的这个简单的类,作为存储字符串匹配算法结果的基础:
/** Match of a single pattern in full to a single text. */
class Match {
uint Tpos;
this(in uint Tpos) { this.Tpos = Tpos; }
override string toString() {
return text("Match: Text@",Tpos);
}
}
这就是事情变得奇怪的地方:
auto m1 = new Match(1), m2 = new Match(1);
writeln(m1.toHash());
writeln(m2.toHash());
writeln(m1 == m2);
印刷
4464528
4464512
false
我看不出为什么默认情况下这两个对象不应该被认为是相等的。我想我可以编写一个自定义toHash()
和opEquals()
函数,但这似乎有点矫枉过正。根据 Andrei Alexandrescu关于 D 编程语言的书(好书!),“默认情况下,哈希是通过使用对象的按位表示来计算的。” 有什么想法吗?