Make Num::from_int a static method

This commit is contained in:
Tim Chevalier 2012-08-14 20:03:31 -07:00
parent f78c906535
commit ccd36439f7
6 changed files with 19 additions and 22 deletions

View File

@ -180,7 +180,7 @@ impl f32: num::Num {
pure fn neg() -> f32 { return -self; }
pure fn to_int() -> int { return self as int; }
pure fn from_int(n: int) -> f32 { return n as f32; }
static pure fn from_int(n: int) -> f32 { return n as f32; }
}
//

View File

@ -207,7 +207,7 @@ impl f64: num::Num {
pure fn neg() -> f64 { return -self; }
pure fn to_int() -> int { return self as int; }
pure fn from_int(n: int) -> f64 { return n as f64; }
static pure fn from_int(n: int) -> f64 { return n as f64; }
}
//

View File

@ -422,7 +422,7 @@ impl float: num::Num {
pure fn neg() -> float { return -self; }
pure fn to_int() -> int { return self as int; }
pure fn from_int(n: int) -> float { return n as float; }
static pure fn from_int(n: int) -> float { return n as float; }
}
#[test]
@ -523,14 +523,14 @@ fn test_traits() {
fn test<U:num::Num>(ten: &U) {
assert (ten.to_int() == 10);
let two = ten.from_int(2);
let two = from_int(2);
assert (two.to_int() == 2);
assert (ten.add(two) == ten.from_int(12));
assert (ten.sub(two) == ten.from_int(8));
assert (ten.mul(two) == ten.from_int(20));
assert (ten.div(two) == ten.from_int(5));
assert (ten.modulo(two) == ten.from_int(0));
assert (ten.add(two) == from_int(12));
assert (ten.sub(two) == from_int(8));
assert (ten.mul(two) == from_int(20));
assert (ten.div(two) == from_int(5));
assert (ten.modulo(two) == from_int(0));
}
test(&10.0);

View File

@ -84,7 +84,7 @@ impl T: num::Num {
pure fn neg() -> T { return -self; }
pure fn to_int() -> int { return self as int; }
pure fn from_int(n: int) -> T { return n as T; }
static pure fn from_int(n: int) -> T { return n as T; }
}
impl T: iter::times {
@ -238,15 +238,15 @@ fn test_interfaces() {
fn test<U:num::Num>(ten: U) {
assert (ten.to_int() == 10);
let two = ten.from_int(2);
let two = from_int(2);
assert (two.to_int() == 2);
assert (ten.add(two) == ten.from_int(12));
assert (ten.sub(two) == ten.from_int(8));
assert (ten.mul(two) == ten.from_int(20));
assert (ten.div(two) == ten.from_int(5));
assert (ten.modulo(two) == ten.from_int(0));
assert (ten.neg() == ten.from_int(-10));
assert (ten.add(two) == from_int(12));
assert (ten.sub(two) == from_int(8));
assert (ten.mul(two) == from_int(20));
assert (ten.div(two) == from_int(5));
assert (ten.modulo(two) == from_int(0));
assert (ten.neg() == from_int(-10));
}
test(10 as T);

View File

@ -1,7 +1,6 @@
/// An interface for numbers.
trait Num {
// FIXME: Cross-crate overloading doesn't work yet. (#2615)
// FIXME: Trait composition. (#2616)
pure fn add(&&other: self) -> self;
pure fn sub(&&other: self) -> self;
@ -11,7 +10,5 @@ trait Num {
pure fn neg() -> self;
pure fn to_int() -> int;
pure fn from_int(n: int) -> self; // FIXME (#2376) Static functions.
// n.b. #2376 is for classes, not traits, but it could be generalized...
static pure fn from_int(n: int) -> self;
}

View File

@ -77,7 +77,7 @@ impl T: num::Num {
pure fn neg() -> T { return -self; }
pure fn to_int() -> int { return self as int; }
pure fn from_int(n: int) -> T { return n as T; }
static pure fn from_int(n: int) -> T { return n as T; }
}
impl T: iter::times {