www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Differences between '==' and 'is' beyond null and overloading

reply "Nicholas Smith" <nmsmith65 gmail.com> writes:
Hello there,

I was wondering what the differences are functionally and 
semantically between '==' and 'is' beyond the two points here and 
my interpretation below. Functionally:
- You must use 'is' to check for a null reference.
- 'is' cannot be overloaded, and it assesses reference types 
based on reference equality and value types based on value 
equality.

Semantically, I've seen that 'is' means identity, as in, "this 
object is physically the same object as another", or "this struct 
is physically the same struct" (which would have an identical 
meaning to == as structs are value types and can't be compared 
any other way).

Equality is then, I believe, meant to mean "this object has the 
same 'value' as this other object", or "this struct has the same 
'value' as this other struct".

But, by default, equality tests if object references are equal, 
perhaps because if a true 'equality' exists then it needs to be 
defined by the programmer via an overload (even through structs 
have an equality test)? Am I on the right track with all of this?
Apr 16 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Nicholas Smith:

 (which would have an identical meaning to == as structs are 
 value types and can't be compared any other way).
You can redefine struct equality.
 But, by default, equality tests if object references are equal,
Currently the equality is a method of object. Bye, bearophile
Apr 16 2013