digitalmars.D.bugs - [Issue 19071] New: core.internal.hash should have non-chained toHash
- d-bugmail puremagic.com (43/43) Jul 09 2018 https://issues.dlang.org/show_bug.cgi?id=19071
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 puremagic.com Reporter: n8sh.secondary 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! } ``` --
Jul 09 2018