cairo by example

operator overloading

Operator overloading is possible in Cairo through the use of particular traits, like Add, Sub, Mul, Div and many others.

For example, for overloading the addition (+) operator of a Vector2 type:

#[derive(PartialEq, Drop)]
struct Vector2 {
    x: felt252,
    y: felt252,
}

impl Vector2Add of Add<Vector2> {
    fn add(lhs: Vector2, rhs: Vector2) -> Vector2 {
            Vector2 { x: lhs.x + rhs.x, y: lhs.y + rhs.y }
        }
}

fn main() {
    let v = Vector2 { x: 1, y: 0 };
    let w = Vector2 { x: 0, y: 1 };
    
    assert(v + w == Vector2 { x: 1, y: 1 }, 'Should be equal.')
}

A full list of overloadable operators and their respective traits can be found in Appendix B of the Cairo Book



Try it out!
  1. Install the toolchain:
    • For macOS and Linux, run our script:
    • curl -sL https://raw.githubusercontent.com/lambdaclass/cairo-by-example/main/build/installer.sh | bash -s 2.2.0
    • For Windows and others, please see the official guide
  2. Run the example:
    1. Copy the example into a op_overloading.cairo file and run with:
    2. %!s(<nil>) op_overloading.cairo

prev (naming convention) next (spans)