Equality
Factor handbook > The language > Objects

Next:Linear order protocol


There are two distinct notions of “sameness” when it comes to objects.

You can test if two references point to the same object (identity comparison). This is rarely used; it is mostly useful with large, mutable objects where the object identity matters but the value is transient:
eq? ( obj1 obj2 -- ? )


You can test if two objects are equal in a domain-specific sense, usually by being instances of the same class, and having equal slot values (value comparison):
= ( obj1 obj2 -- ? )


A third form of equality is provided by number=. It compares numeric value while disregarding types.

Custom value comparison methods for use with = can be defined on a generic word:
equal? ( obj1 obj2 -- ? )


Utility class:
identity-tuple


An object can be cloned; the clone has distinct identity but equal value:
clone ( obj -- cloned )