[Issue 19071] New: core.internal.hash should have non-chained toHash overloads
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jul 9 16:46:27 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=19071
Issue ID: 19071
Summary: core.internal.hash should have non-chained toHash
overloads
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: druntime
Assignee: nobody at puremagic.com
Reporter: n8sh.secondary at hotmail.com
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!
}
```
--
More information about the Digitalmars-d-bugs
mailing list