The `==' operator (Re: [nem-en] Nemerle 0.2.0 released)

Michal Moskal malekith at pld-linux.org
Mon Sep 13 19:04:43 CEST 2004


On Mon, Sep 13, 2004 at 12:36:17PM -0400, Mark Friedman wrote:
> I'm curious about the rationale for having the '==' operator not
> fallback to reference equality?  I guess that it's not entirely clear
> to me what the '==' operator actually means in Nemerle.  

It works the same as in C#, that it is statically resolved whatever class
of the left or right parameter (or any base class thereof) has an overload
on ==. Some classes (like System.String or System.Decimal) have it.

However C# has an additional fallback case -- if there is no matching
overload, reference equality is used. That is == checks if objects on
both sides are physically the same.

Now this dual behavior is confusing for me -- what does it mean if I use
==? It depends. It is often conceptual error to use == on classes that
don't overload it. In fact, after this was changed a few such errors was
spotted in the compiler. For me this fallback is against static type
safety.

But of course, there need to be a way to check for physical reference
equality, that's why it still work on values of the System.Object type.

> Is it
> syntactic sugar for some method on System.Object (or its Nemerle
> proxy)?

Nope. Though maybe it would have been wiser for == to mean to call
Equals() method... But this would be confusing for C# programmers.

> Is there some other method that returns true for reference
> equality?  Is there an operator corresponding to that method?

Yes, (x : object) == (y : object). There is also
System.Object.ReferenceEquals() method in the Base Class Library.

-- 
: Michal Moskal :: http://www.kernel.pl/~malekith :: GCS !tv h e>+++ b++
: ::: Logic is a nice contrast to the Real World. :: UL++++$ C++ E--- a?




More information about the devel-en mailing list