Mojo trait
Equatable
A type which can be compared for equality with other instances of itself.
The Equatable trait has a default implementation of __eq__() that uses
reflection to compare all fields. This means simple structs can conform to
Equatable without implementing any methods:
@fieldwise_init
struct Point(Equatable):
var x: Int
var y: Int
var p1 = Point(1, 2)
var p2 = Point(1, 2)
print(p1 == p2) # TrueAll fields must conform to Equatable. Override __eq__() for custom
equality semantics.
Note: The default implementation performs memberwise equality comparison. This may not be appropriate for types containing floating-point fields (due to NaN semantics) or types requiring custom equality logic.
Implemented traits
AnyType,
ImplicitlyDestructible
comptime members
__del__is_trivial
comptime __del__is_trivial
A flag (often compiler generated) to indicate whether the implementation of __del__ is trivial.
The implementation of __del__ is considered to be trivial if:
- The struct has a compiler-generated trivial destructor and all its fields
have a trivial
__del__method.
In practice, it means that the __del__ can be considered as no-op.
Provided methods
__eq__
__eq__(self: _Self, other: _Self) -> Bool
Define whether two instances of the object are equal to each other.
The default implementation uses reflection to compare all fields for
equality. All fields must conform to Equatable.
Args:
- other (
_Self): Another instance of the same type.
Returns:
Bool: True if the instances are equal according to the type's definition
of equality, False otherwise.
__ne__
__ne__(self: _Self, other: _Self) -> Bool
Define whether two instances of the object are not equal to each other.
Args:
- other (
_Self): Another instance of the same type.
Returns:
Bool: True if the instances are not equal according to the type's
definition of equality, False otherwise.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!