Trait in prelude::ops auto-imported

Add

trait Add<T>
Source: compiler/src/builtin_docs/prelude/ops.hy

The addition operator +.

a + b on a type with an Add instance calls add(a, b). int and float have builtin instances (compiled to machine adds, not calls). String concatenation with + is a separate rule and does not go through Add. In a generic function, T: Add (or T: Num) allows + on T.

Required methods#

NameDescription
addReturns a + b.

Examples#

use tests::assert;
class V2 {
    pub x: int,
    pub y: int,
}

impl Add for V2 {
    pub fn add(V2 a, V2 b) -> V2 {
        return new V2(a.x + b.x, a.y + b.y);
    }
}

fn main() {
    let p = new V2(1, 2) + new V2(10, 20);
    assert!(p.x == 11);
    assert!(p.y == 22);
}

See also#

  • int
  • float
  • Sub: The subtraction operator -.
  • Mul: The multiplication operator *.
  • Div: The division operator /.
  • Neg: The unary negation operator -x.
  • Rem: The remainder operator %.
  • Pow: The exponentiation operator **.
↑ ↓ navigate ↵ open