www.digitalmars.com         C & C++   DMDScript  

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


          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 puremagic.com
          Reporter: verylonglogin.reg 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

Nov 24 2016