Integer addition!
This commit is contained in:
parent
6baf62e8c2
commit
6ca01cbb5a
3 changed files with 38 additions and 1 deletions
36
types/src/number/add.rs
Normal file
36
types/src/number/add.rs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
|
||||||
|
|
||||||
|
use std::ops::Add;
|
||||||
|
use super::{Int, Flt, Real};
|
||||||
|
|
||||||
|
impl Add for Real {
|
||||||
|
type Output = Real;
|
||||||
|
|
||||||
|
fn add(self, other: Real) -> Real {
|
||||||
|
match other {
|
||||||
|
Real::Integer(v) => self.add_integer(v),
|
||||||
|
_ => Real::Integer(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Real {
|
||||||
|
fn add_integer(self, v_other: Int) -> Real {
|
||||||
|
match self {
|
||||||
|
Real::Integer(v) => Real::Integer(v + v_other),
|
||||||
|
_ => Real::Integer(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use number::Real;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn integer_add_works() {
|
||||||
|
let result = Real::Integer(3) + Real::Integer(5);
|
||||||
|
assert_eq!(result, Real::Integer(8));
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@
|
||||||
/// Scheme numbers are complex, literally.
|
/// Scheme numbers are complex, literally.
|
||||||
|
|
||||||
pub mod real;
|
pub mod real;
|
||||||
|
mod add;
|
||||||
|
|
||||||
pub use self::real::Real;
|
pub use self::real::Real;
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ impl IsBool for Real { }
|
||||||
impl IsChar for Real { }
|
impl IsChar for Real { }
|
||||||
|
|
||||||
impl IsNumber for Real {
|
impl IsNumber for Real {
|
||||||
fn is_number(&self) -> 9bool { true }
|
fn is_number(&self) -> bool { true }
|
||||||
|
|
||||||
fn is_integer(&self) -> bool {
|
fn is_integer(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue