Page 1 of 1

== and equals()

PostPosted: Thu Jul 22, 2010 5:27 pm
by nevdelap
Hey guys,

I'm writing a Cairo app and when I couldn't compare PointD's (2D point with doubles) I thought I had to add an equals extension method because == couldn't be used, but then discovered that it already has an equals so that the equals here works, but the == doesn't.

Code: Select all
use Cairo from Mono.Cairo

class Program

   def main
      assert PointD(1, 2).equals(PointD(1, 2))
      assert PointD(1, 2) == PointD(1, 2) # error: Operator "==" cannot be applied to operands of type "Cairo.PointD" and "Cairo.PointD"


I was under the impression that the equals() was used to implement the == and <>.

Can you explain what's going on?

Thanks,

Nev

Re: == and equals()

PostPosted: Fri Jul 23, 2010 4:58 am
by hopscc
From a quick code skim, For reference types it uses .Equals eventually but on its voyage to that it looks like its doing a check for existence of .CompareTo ( IComparable) which is
where the error message is coming from..
( are you sure it sez 'cannot be applied' and not 'cannot be compared' ?)

This seems a bit of a miss for syntax checking vs codegen but chuck can probably clarify.

Re: == and equals()

PostPosted: Fri Jul 23, 2010 7:56 am
by nevdelap
Yep. That's def a copy and paste of the message.

Re: == and equals()

PostPosted: Mon Aug 23, 2010 11:54 pm
by hopscc
Hey Chuck, can ya take a quick look at this and give us an idea whether either the (compiler) code is wrong ( . -> CompareTo -> Equals) or the assumption around 'equals' is..

Is it just that '==' needs 'CompareTo' rather than an 'Equals' override ?

Re: == and equals()

PostPosted: Tue Aug 24, 2010 1:52 pm
by nevdelap
Once I understood that CompareTo was used for ==, <> from Mike's post, I wondered if for things where <, > etc aren't meaningful it would preclude using IComparable/CompareTo. ?

Re: == and equals()

PostPosted: Thu Aug 26, 2010 5:14 am
by Charles
Sorry I missed this thread. I'm traveling today and visiting family for the next several days. Maybe I can look into this tomorrow/Friday.

Re: == and equals()

PostPosted: Thu Sep 02, 2010 9:59 pm
by Charles
One thing I notice is that this is a C# error. And PointD is a struct so we may have a bug here in Cobra's treatment of == with structs. It looks like Cobra accepts "a == b" for structs, but does not generate the C# for that expression that would make the C# compiler happy.

Re: == and equals()

PostPosted: Thu Sep 02, 2010 10:44 pm
by Charles
This is now fixed.