Equals method

From CodeCodex

Implementations[edit]

C++[edit]

class Foo {
private:
  string str;
  int i;
  [...]
public:
  bool operator==(const Foo& x) {
    if (str != x.str) return false;
    if (i != x.i) return false;
    [...]
    return true;
  }
  [...]
}

Haskell[edit]

Haskell has a type class called Eq which says that a type can be compared for equality. It specifies the operators "==" (equals) and "/=" (not equals); you only need to override one of them.

If you want Haskell to automatically generate a default equality operator for your type, you can simply "derive" the Eq class when you define the type:

data Foo = Bar | Baz deriving (Eq)

If you want to provide your own equality operator, you can instead instantiate the Eq class yourself:

instance Eq Foo where
  Bar == Bar  = True
  Baz == Baz  = True
  _   == _    = False

Java[edit]

class Foo {
   private String name;
   private int i;
   [...]

   public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Foo)) return false; 

        Foo other = (Foo) o;

        if (name != null ? !name.equals(other.name) : other.name != null) return false;
        if (i != other.i) return false;
        [...]
        return true;
   }
   // Note: after overriding the equals() method, you must also override the hashCode()
   // method to be consistent with equals() (i.e. two objects' hashCode() need to be the
   // same if they are equal.
}

Objective-C[edit]

@interface Foo {
   NSString *name;
   int i;
}
[...]
@end

@implementation Foo
- (BOOL)isEqual:(id)o {
     if (self == o) return YES;
     if (![o isKindOfClass:[Foo class]]) return NO; 

     Foo *other = (Foo *) o;

     if (name != nil ? ![name isEqual:other->name] : other->name != nil) return NO;
     if (i != other->i) return NO;
     [...]
     return YES;
}
// Note: after overriding the isEqual: method, you must also override the hash
// method to be consistent with isEqual: (i.e. two objects' hashes need to be the
// same if they are equal.
@end

OCaml[edit]

OCaml has built-in polymorphic structural equality:

# (=);;
- : 'a -> 'a -> 'bool = <fun>

which has the advantage that you do not need to declare equality for types but has the disadvantage that you cannot overload equality for certain types (e.g. balanced binary trees, where structural equality and semantic equality are not the same). Instead, you must discipline yourself to use the appropriate equality function.

Ruby[edit]

Equality―At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning.

Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) if a is the same object as b).

The eql? method returns true if obj and anObject have the same value. Used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses normally continue this tradition, but there are exceptions. Numeric types, for example, perform type conversion across ==, but not across eql?

Original source : [1]

class Foo
  attr_accessor :value
  
  # The equivalence confirmation
  def ==(other)
    @value == other.value
  end
  
  # The equivalence confirmation (Numeric doesn't do a type conversion).
  def eql?(other)
    @value.eql?(other.value)
  end
end

a = Foo.new
a.value = 1
b = Foo.new
b.value = 1.0
p a == b        #=> true
p a.equal?(b)   #=> false
p a.eql?(b)     #=> false

Standard ML[edit]

Standard ML has built-in polymorphic structural equality:

 - op =;
 stdIn:1.1-1.5 Warning: calling polyEqual
 val it = fn : ''a * ''a -> bool

You can only use "=" on types that are declared to be "eqtypes". The two quotes before the type variable denotes that the type variable must be an eqtype. Most common types are eqtypes, and most datatypes that you define should be declared to be eqtypes. Note that "real" (the floating-point type) is not an eqtype, so cannot be compared with "=".

Another disadvantage with this setup is that you cannot overload equality for certain types (e.g. balanced binary trees, where structural equality and semantic equality are not the same). If you wanted to define a custom equality, you would typically make your type not an eqtype (so that they won't accidentally use "="), and then define a separate operator, like "==".