Bug 19071 – core.internal.hash should have non-chained toHash overloads
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-07-09T16:46:27Z
Last change time
2018-08-15T14:13:04Z
Assigned to
No Owner
Creator
Nathan S.
Comments
Comment #0 by n8sh.secondary — 2018-07-09T16:46:27Z
Each hash function in `core.internal.hash` currently has the form:
```
size_t hashOf(T)(auto ref T val, size_t seed = 0);
```
The idea behind the second `seed` argument was to allow "chaining", e.g. `hashOf(z, hashOf(x, hashOf(y)))`. The downside is that for any type for which `hashOf` is not a wrapper around `bytesHash` this performs additional work which can be avoided if chaining is not needed.
To fix this instead of a single function with a default argument there should be two overloads:
```
size_t hashOf(T)(auto ref T val, size_t seed);
size_t hashOf(T)(auto ref T val);
```
Aside from avoiding unnecessary work for cases like `hashOf(uint(x))`, this would also cause `hashOf(y)` to be the same as `y.toHash`:
```
unittest
{
static struct Record
{
size_t id; int[string] properties;
size_t toHash() const @nogc nothrow pure @safe { return id; }
}
Record record = { id: 3 };
assert(record.toHash() == hashOf(record)); // Currently fails!
}
```
Comment #1 by github-bugzilla — 2018-08-15T14:13:04Z