Bug 16764 – `hashOf` is misleading, error-prone, and useless
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-11-24T18:12:00Z
Last change time
2017-01-16T23:24:41Z
Assigned to
nobody
Creator
verylonglogin.reg
Comments
Comment #0 by verylonglogin.reg — 2016-11-24T18:12:12Z
In our root `object` module we have a function `hashOf` [1] with correct definition: "to calculate hash of raw byte representation of given object" (at posting time, see [2]).
This function should not be there because:
1. It is misleading (and as a result error-prone) because user doesn't expect that logical object structure is not considered and this function operates with raw representation bytes.
2. Has signature allowing incorrect usage (and as a result the function is extremely error-prone).
3. It is not generally useful because primary purpose of hash functions is to calculate hash of byte array with dynamic length (`void[]`) and this function only accept data with size known at compilation time.
The code illustrating the issue (#1 and #2 from list above):
---
void main()
{
// #1:
hashOf(5); // #1: hash of raw int bytes, different on different systems
real r;
hashOf(r); // #1: hash of raw bytes of real including padding
hashOf(MyStruct()); // #1: hash of raw bytes of struct including padding and ignoring `toHash` function
int[] arr = [1];
hashOf(arr); // #1: hash of raw bytes of ptr+length
assert(hashOf(arr) == hashOf([1])); // #1: as a result this fails
// #2:
hashOf(arr.ptr, arr.length); // #2: hash of raw bytes of ptr with seed set to length
}
---
[1] http://dlang.org/phobos/object.html#.hashOf
[2] https://github.com/dlang/druntime/blob/7962fb8acaeb0008c531d1ae170cd15ff59558ea/src/object.d#L3203