[Issue 16764] New: `hashOf` is misleading, error-prone, and useless

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Nov 24 10:12:12 PST 2016


https://issues.dlang.org/show_bug.cgi?id=16764

          Issue ID: 16764
           Summary: `hashOf` is misleading, error-prone, and useless
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: verylonglogin.reg at gmail.com

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

--


More information about the Digitalmars-d-bugs mailing list