-
Notifications
You must be signed in to change notification settings - Fork 2
Atom equality
In Java, java.lang.Object
class, known as the root of all the objects hierarchy, has two methods for defining equality semantics:
public boolean equals(Object o);
public int hashCode();
The presense of these methods considered as a dirty solution, for several of reasons:
- One object may have different kinds of equality (full equality, equality by key, equality by reference, etc.), while the method is only one.
- Method equals accepts object of type
java.lang.Object
, which means that where is no way to implement this method without casting andinstanceof
.
Atom concept hides this mess by applying special semantics to the Atom equality. Formally, Atoms considered equal if and only if they represent exactly the same behavior. It means that a call of a method on two equal atoms always cause execution of exactly the same code.
Practically, Java Atoms equality is defined by the following recursive rule: two Atoms are equal if they are the instance of the same class and their fields are equal. Taking into account the fact that all the fields are immutable in Atom, the formal requirement is satisfied (TODO: moar proof). Also please note that for two Atoms to be equal, they don't nesessary have to be of exactly the same type. It is enough that they are instance of the same type. This assumption is used for Atom aliases equality (TODO: add example).
OO-atom project is able to generate the equals
and hashCode
methods implicitly for each class which follows Atom specification.