Operator Overloading
In Cairo, many operators can be overloaded via traits. That is, some operators can be used to accomplish different tasks based on their input arguments. This is possible because operators are syntactic sugar for method calls. For example, the + operator in a + b calls the add method (as in a.add(b)). This add method is part of the Add trait. Hence, the + operator can be used by any implementor of the Add trait.
A list of the traits that overload operators can be found in core::ops and core::traits.
struct Potion {
health: u64,
mana: u64,
}
// The `Add` trait is used to specify the functionality of `+`.
// Here, we make `Add<Potion>` - the trait for addition with a LHS and RHS of type `Potion`.
// The following block implements the operation: Potion + Potion = Potion
impl PotionAdd of Add<Potion> {
fn add(lhs: Potion, rhs: Potion) -> Potion {
Potion { health: lhs.health + rhs.health, mana: lhs.mana + rhs.mana }
}
}
fn main() {
let health_potion: Potion = Potion { health: 100, mana: 0 };
let mana_potion: Potion = Potion { health: 0, mana: 100 };
let super_potion: Potion = health_potion + mana_potion;
// Both potions were combined with the `+` operator.
assert(super_potion.health == 100, '');
assert(super_potion.mana == 100, '');
}