Replies: 1 comment
-
Abstract numbersBelow is a set of traits for different number systems: use ingot::ops::arith::{
Add, Sub, Mul, Div, Neg,
}
pub trait Num: Add + Sub + Mul + Div + Neg { }
pub trait Complex<N>: Num {
fn re() -> N
fn im() -> N
}
pub trait Real: Num { }
pub trait Int: Num { }
pub trait Frac<N>: Num {
fn nu() -> N
fn de() -> N
}
pub trait Nat: Num { } Example implementation and usage for impl Add for i8 { fn add(self, rhs: Self) -> Self { self + rhs } }
impl Sub for i8 { fn sub(self, rhs: Self) -> Self { self - rhs } }
impl Mul for i8 { fn mul(self, rhs: Self) -> Self { self * rhs } }
impl Div for i8 { fn div(self, rhs: Self) -> Self { self / rhs } }
impl Neg for i8 { fn neg(self) -> Self { -rhs } }
impl Num for i8 { }
impl Int for i8 { }
fn my_math_fn1<Z: Int>(n: Z) { }
fn my_math_fn2<N: Num>(n: N) { }
fn foo() {
let n: i8 = 42
my_math_fn1(n)
my_math_fn2(n)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This thread is for discussing standard library design.
Beta Was this translation helpful? Give feedback.
All reactions