www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Comparing Instances of Classes

reply DRex <armstronga94 hotmail.com> writes:
Hi,

I am trying to compare two instances of a class.  I created a 
test program to try this, but every method I use to compare the 
instances always returns false.

this is my code to test comparison

class A
{
     this()
     {

     }
}

void main()
{
     A a = new A();
     A a2 = new A();

     writeln(equals(a, a2));
}

bool equals(Object obj1, Object obj2)
{
     return (obj1 is obj2);
}

I have tried 'a is a2', I have tried 'a1 == a2' and many other 
ways (including opEquals from object.d) among other things and 
every single time the comparison returns false.  The comparison 
always fails and never returns true.

I am trying to do something like Object.equals(Object o) in java, 
but so far, no success.

Am I missing something here?
Mar 10
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 10 March 2017 at 16:08:05 UTC, DRex wrote:
 Am I missing something here?
Yeah, you need to implement a custom equality operator. class A { int member; override bool opEquals(const A rhs) { return this.member == rhs.member; // and other members that need to be equal } } The default opEquals sees if they are the same *instance* (same as `a is b`), and does not look at contents. You need to define that yourself.
Mar 10
parent reply DRex <armstronga94 hotmail.com> writes:
On Friday, 10 March 2017 at 16:13:21 UTC, Adam D. Ruppe wrote:
 On Friday, 10 March 2017 at 16:08:05 UTC, DRex wrote:
 Am I missing something here?
Yeah, you need to implement a custom equality operator. class A { int member; override bool opEquals(const A rhs) { return this.member == rhs.member; // and other members that need to be equal } } The default opEquals sees if they are the same *instance* (same as `a is b`), and does not look at contents. You need to define that yourself.
I tried the above class A, and now the compiler fails with the following error: Error: function app.A.opEquals does not override any function, did you mean to override 'object.Object.opEquals'? My A class appears exactly as mentioned in your comment...
Mar 10
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 10 March 2017 at 16:22:18 UTC, DRex wrote:
 Error: function app.A.opEquals does not override any function, 
 did you mean to override 'object.Object.opEquals'?
Oh sorry, maybe I messed up the const. Try: override bool opEquals(A rhs) { ... } and if the compiler still complains change the A to Object and cast it inside (but I'm pretty sure that will work, I think it is just const it is picky about)
Mar 10
parent reply DRex <armstronga94 hotmail.com> writes:
On Friday, 10 March 2017 at 16:30:00 UTC, Adam D. Ruppe wrote:
 On Friday, 10 March 2017 at 16:22:18 UTC, DRex wrote:
 Error: function app.A.opEquals does not override any function, 
 did you mean to override 'object.Object.opEquals'?
Oh sorry, maybe I messed up the const. Try: override bool opEquals(A rhs) { ... } and if the compiler still complains change the A to Object and cast it inside (but I'm pretty sure that will work, I think it is just const it is picky about)
Thanks. I'm fairly new to D, but this seems to be quite a pain in the rear for a simple comparison of instances of classes...really odd that comparing instances of classes in D requires that messing around when D seems all about simplifying things...
Mar 10
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 10 March 2017 at 16:36:17 UTC, DRex wrote:
 I'm fairly new to D, but this seems to be quite a pain in the 
 rear for a simple comparison of instances of classes...really 
 odd that comparing instances of classes in D requires that 
 messing around when D seems all about simplifying things...
There often is no sensible default comparison for class contents (now structs on the other hand do have a default comparison that usually works, but structs don't have to worry about polymorphism), so you just need to specify what fields actually matter to your code...
Mar 10
parent reply Whatsthisnow <armstronga94 hotmail.com> writes:
On Friday, 10 March 2017 at 16:47:47 UTC, Adam D. Ruppe wrote:
 On Friday, 10 March 2017 at 16:36:17 UTC, DRex wrote:
 I'm fairly new to D, but this seems to be quite a pain in the 
 rear for a simple comparison of instances of classes...really 
 odd that comparing instances of classes in D requires that 
 messing around when D seems all about simplifying things...
There often is no sensible default comparison for class contents (now structs on the other hand do have a default comparison that usually works, but structs don't have to worry about polymorphism), so you just need to specify what fields actually matter to your code...
I guess i am just too used to the java way of x.equals(object) which at the source is exactly 'return this == object'
Mar 10
parent reply Meta <jared771 gmail.com> writes:
On Friday, 10 March 2017 at 17:08:42 UTC, Whatsthisnow wrote:
 I guess i am just too used to the java way of x.equals(object) 
 which at the source  is exactly 'return this == object'
Java would return false here too, though, if it actually did `this == object` in its default compare method. If I remember correctly, comparing two objects with == in Java compares their addresses, not their contents.
Mar 10
parent DRex <armstronga94 hotmail.com> writes:
On Friday, 10 March 2017 at 20:27:09 UTC, Meta wrote:
 On Friday, 10 March 2017 at 17:08:42 UTC, Whatsthisnow wrote:
 I guess i am just too used to the java way of x.equals(object) 
 which at the source  is exactly 'return this == object'
Java would return false here too, though, if it actually did `this == object` in its default compare method. If I remember correctly, comparing two objects with == in Java compares their addresses, not their contents.
I must be losing my mind then
Mar 10
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/10/2017 08:22 AM, DRex wrote:

 Error: function app.A.opEquals does not override any function, did you
 mean to override 'object.Object.opEquals'?

 My A class appears exactly as mentioned in your comment...
FWIW, here's some other info: http://ddili.org/ders/d.en/object.html#ix_object.opEquals Ali
Mar 10