Associative arrays in D and default comparators

Ivan Senji ivan.senji_REMOVE_ at _THIS__gmail.com
Fri Sep 8 13:56:45 PDT 2006


xs0 wrote:
> Walter Bright wrote:
>> Ivan Senji wrote:
>>> Yes: here is a suggestion: remove opCmp from Object. I think the only 
>>> reason it is there is that when AAs where first implemented templates 
>>> weren't where they are now so there was no way to check if an object 
>>> has opCmp. These days a template version of AAs would be much better, 
>>> and it would (if I'm not mistaken) remove the need for opCmp to be in 
>>> Object.
>>
>> While it'd be fun to offer a templated version of AAs, I feel the core 
>> capabilities should be available to the user without needing 
>> templates. This is because many programmers are not comfortable with 
>> them.
> 
> But if you hide the implementation via AA syntax sugar, those many 
> programmers won't have to deal with templates at all?

Yes, everything would remain as it is now, except better.

> 
>> Can you give an example of a class that could not have a meaningful 
>> opCmp implementation that one would want to put into an AA?
> 
> Object :)

LOL

> 
> OK, that may be a bit Java-ish, but the simplest way to obtain a unique 
> key/identifier is simply "new Object()". It works in HashMaps, is equal 
> only to itself, and that's about everything you need in some cases.
> 
> ----
> 
> I think opCmp should be removed from Object, it causes nothing but grief:
> - it errors at runtime if not implemented, instead of at compile time
> - it requires you to cast the parameter
> - can never be inlined

Thanks xs0, you have put my exact thoughts into words :)






More information about the Digitalmars-d mailing list