www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - AA with class keys compared with identity instead of opEquals

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
How do I defined an AA with class as key where keys are compared 
using `is` instead of `opEquals`? Do I have to store the key as a 
`void*`?
Sep 27 2020
next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Sunday, 27 September 2020 at 18:39:10 UTC, Per Nordlöw wrote:
 How do I defined an AA with class as key where keys are 
 compared using `is` instead of `opEquals`? Do I have to store 
 the key as a `void*`?
By looking at object.d and aaA.d of druntime, I d say you don't need to use void*. Object class has required infrastructure ready for using classes aa keys (have not tried though). Object class has both toHash and opEquals already implemented.
Sep 27 2020
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 27 September 2020 at 18:56:15 UTC, Ferhat Kurtulmuş 
wrote:
 By looking at object.d and aaA.d of druntime, I d say you don't 
 need to use void*. Object class has required infrastructure 
 ready for using classes aa keys (have not tried though). Object 
 class has both toHash and opEquals already implemented.
That's gonna be much slower. And I need key's to be checked for equivalence ,not equality.
Sep 27 2020
prev sibling parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 27 September 2020 at 18:39:10 UTC, Per Nordlöw wrote:
 How do I defined an AA with class as key where keys are 
 compared using `is` instead of `opEquals`? Do I have to store 
 the key as a `void*`?
I got a good answer at https://dlang.slack.com/archives/C1ZDHBB2S/p1601234030016700
Sep 27 2020
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Sunday, 27 September 2020 at 19:37:10 UTC, Per Nordlöw wrote:
 On Sunday, 27 September 2020 at 18:39:10 UTC, Per Nordlöw wrote:
 How do I defined an AA with class as key where keys are 
 compared using `is` instead of `opEquals`? Do I have to store 
 the key as a `void*`?
I got a good answer at https://dlang.slack.com/archives/C1ZDHBB2S/p1601234030016700
Sorry I did not understand what you meant. It would be better if you share the answer here. I found this also https://dlang.org/library/std/traits/key_type.html, but I dont know if it is related.
Sep 27 2020
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 27 September 2020 at 19:45:07 UTC, Ferhat Kurtulmuş 
wrote:
 Sorry I did not understand what you meant. It would be better 
 if you share the answer here. I found this also 
 https://dlang.org/library/std/traits/key_type.html, but I dont 
 know if it is related.
In the case where the AA-KeyType is a class (which is represented as a pointer in D) I want the equality (opEquals) and the hashing (toHash) of the AA to compare and hash the pointers themselves, not the fields the classes points to.
Sep 28 2020
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 28 September 2020 at 08:26:15 UTC, Per Nordlöw wrote:
 In the case where the AA-KeyType is a class (which is 
 represented as a pointer in D) I want the equality (opEquals) 
 and the hashing (toHash) of the AA to compare and hash the 
 pointers themselves, not the fields the classes points to.
For D-classes this is the default. For extern(C++) classes it's not defined. That's why I added the struct wrapper on top of an extern(C++) class.
Sep 28 2020