Forums

== and equals()

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

== and equals()

Postby nevdelap » Thu Jul 22, 2010 5:27 pm

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
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: == and equals()

Postby hopscc » Fri Jul 23, 2010 4:58 am

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.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: == and equals()

Postby nevdelap » Fri Jul 23, 2010 7:56 am

Yep. That's def a copy and paste of the message.
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: == and equals()

Postby hopscc » Mon Aug 23, 2010 11:54 pm

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 ?
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: == and equals()

Postby nevdelap » Tue Aug 24, 2010 1:52 pm

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. ?
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: == and equals()

Postby Charles » Thu Aug 26, 2010 5:14 am

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: == and equals()

Postby Charles » Thu Sep 02, 2010 9:59 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: == and equals()

Postby Charles » Thu Sep 02, 2010 10:44 pm

This is now fixed.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 1 guest