ProtoObject and comparison for equality and ordering
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue May 14 21:05:02 UTC 2019
On 5/14/19 9:37 PM, Mike Franklin wrote:
> On Tuesday, 14 May 2019 at 19:34:12 UTC, Andrei Alexandrescu wrote:
>> In designing ProtoObject and comparison for equality and ordering,
>> we've assumed all class objects are supposed to be comparable
>> (including ProtoObject themselves).
>
> What's the rationale for that?
Pretty much principle of least surprise for existing D users.
> And are you talking about reference
> equality or value equality?
Depends on how the user implements it. The intent is to support
meaningful comparison of the content of objects.
> IMO, objects should only support reference equality out of the box.
So that means "x is y" works but not "x == y"?
>> This is in keeping with Java, C#, and existing D where Object has
>> built-in means for comparison.
>
> Classes in C# only support reference equality out of the box:
> https://dotnetfiddle.net/AOQ0Ry
Affirmative. Same in Java, is that correct?
> I would like to distinguish between reference equality and value
> equality. Reference equality, I think, should be done with the `is`
> operator, and should probably work out of the box. And I like the idea
> of users opting into any feature. If users want to support value
> equality, they should implement `opEquals`. If they want comparability
> they should implement `opCmp`. If they want to support hashability, the
> should be required to implement a `getHash` or something along that line
> of thinking.
Sounds good. If we go with the notion "you can't key a built-in
hashtable on any class type" that should work.
More information about the Digitalmars-d
mailing list