Thread overview
[Issue 10525] New: Struct as key in Associative array ignores value semantics
Jul 02, 2013
Michal Minich
Jul 11, 2013
Michal Minich
July 02, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10525

           Summary: Struct as key in Associative array ignores value
                    semantics
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: michal.minich@gmail.com


--- Comment #0 from Michal Minich <michal.minich@gmail.com> 2013-07-02 02:06:31 PDT ---
DMD 2.063.2

struct S { char[] str; }

void main ()
{
    auto s1 = S(cast(char[])"abc");
    auto s2 = S(cast(char[])"Xbc");

    // indirect members in structs are compared by value
    assert (s1 != s2);  // ok, structs are compared not equal
    s2.str[0] = 'a';
    assert (s1 == s2);  // ok, structs are compared equal

    // not so in AA
    auto aa = [s1 : 1];

    auto s1aa = s1 in aa;
    assert (s1aa);

    auto s2aa = s2 in aa;
    assert (s2aa);   // fails, but should pass
                     // s2 should be found in aa the same way as s1
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10525


hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx


--- Comment #1 from hsteoh@quickfur.ath.cx 2013-07-08 15:09:40 PDT ---
This is not really a bug. Structs by default are bitwise-compared.

If you want structs to be deep-compared, you need to define toHash and opCmp:

struct S {
    char[] str;

    size_t toHash() const {
        // Just use arrays' builtin hash function, no need to reinvent your own
        return typeid(str).getHash(&str);
    }

    // Note: this exact function signature must be used;
    // DMD is currently very picky about this.
    int opCmp(ref const S s) const {
        return typeid(str).compare(&str, &s.str);
    }
}

Once these two pieces are in place, your struct should work correctly as AA key.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 11, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10525



--- Comment #2 from Michal Minich <michal.minich@gmail.com> 2013-07-11 03:29:28 PDT ---
(In reply to comment #1)
> This is not really a bug. Structs by default are bitwise-compared.
This is a bug. structs are deeply compared by default as of last dmd release. I included an example of that in the bug report code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------