Trait in prelude::ops auto-imported

Num

trait Num<T: Add + Sub + Mul + Div + Neg + Rem + Pow>
Source: compiler/src/builtin_docs/prelude/ops.hy

The numeric bundle: Add, Sub, Mul, Div, Neg, Rem and Pow.

Num has no methods of its own. A bound T: Num lets a generic function use every arithmetic operator on T. int and float are Num (byte is not). A hand-written impl Num for X needs an instance of each of the seven traits first.

Examples#

use tests::assert;
fn square_plus<T: Num>(T a, T b) -> T {
    return a * a + b;
}

fn main() {
    assert!(square_plus(3, 1) == 10);
    assert!(square_plus(1.5, 0.75) == 3.0);
}

See also#

↑ ↓ navigate ↵ open