Trait in prelude::ops auto-imported

Neg

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

The unary negation operator -x.

-x on a type with a Neg instance calls neg(x). int and float have builtin instances; negating the smallest int panics with integer overflow.

Required methods#

NameDescription
negReturns -a.

Examples#

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

impl Neg for V2 {
    pub fn neg(V2 a) -> V2 {
        return new V2(-a.x, -a.y);
    }
}

fn main() {
    let n = -new V2(1, -2);
    assert!(n.x == -1);
    assert!(n.y == 2);
}

See also#

  • int
  • float
  • Add: The addition operator +.
  • Sub: The subtraction operator -.
  • Mul: The multiplication operator *.
  • Div: The division operator /.
  • Rem: The remainder operator %.
  • Pow: The exponentiation operator **.
↑ ↓ navigate ↵ open