How is opEquals used in toHash
PinDPlugga
a at a.com
Tue May 18 10:14:26 UTC 2021
In the solution to one of the exercises in Programming in D the
unittests fail with respect to the toHash implementation.
Here is a link to the full solution provided:
https://run.dlang.io/gist/99ddf791f86aaa9d333d032166aadcb9?args=-unittest%20-main
and the link to the relevant section in the book:
https://ddili.org/ders/d.en/object.html
so while the unittest for equality passes, the next test with
`in` is where the failure occurs:
```D
// ...
assert(area1 == area2); // Passes
double[TriangularArea] areas;
areas[area1] = 1.25;
assert(area2 in areas); // Fails
// ...
```
the issue must be with the toHash function given
``` D
override size_t toHash() const {
/* Since the 'points' member is an array, we can take
* advantage of the existing toHash algorithm for
* array types. */
return typeid(points).getHash(&points);
}
```
I looked into the object library documentation and saw that the
template getHash calls hashOf which calls
core.internal.hash.hashOf etc.
But what I do not understand is why opEquals is necessary and
where in the implementation of toHash it plays its role? Since
area1 and area2 have different static arrays of Points I
understand why `typeid(points).getHash(&points)` would produce
different values for each, but it seems like the opEquals should
play a part in making them produce the same hash.
More information about the Digitalmars-d-learn
mailing list