Last active
December 11, 2015 18:58
-
-
Save auroranockert/4644880 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! Q(($T:ty, $f:expr, $name:ident) => { | |
struct $name { | |
value: $T | |
} | |
impl $name { | |
pure fn add(&self, other: &$name) -> $name { | |
$name { value: self.value + other.value } | |
} | |
} | |
}) | |
Q!(i8, 8, Q8) | |
fn main() { | |
let a = Q8 { value: 4 }; | |
let b = Q8 { value: 8 }; | |
io::println(fmt!("Q8: %?", &a.add(&b))) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! Q(($T:ty, $f:expr, $name:ident) => ( | |
struct $name { | |
value: $T | |
} | |
impl $name: Add<$name, $name> { | |
pure fn add(&self, other: &$name) -> $name { | |
$name { value: self.value + other.value } | |
} | |
} | |
)) | |
Q!(i8, 8, Q8) | |
fn main() { | |
let a = Q8 { value: 4 }; | |
let b = Q8 { value: 8 }; | |
io::println(fmt!("Q8: %?", a + b)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test.rs:67:31: 67:36 error: binary operation + cannot be applied to type `Q8` | |
test.rs:67 io::println(fmt!("Q8: %?", a + b)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! Q(($T:ty, $f:expr, $name:ident) => ( | |
struct $name { | |
value: $T | |
} | |
)) | |
impl Q8: Add<Q8, Q8> { | |
pure fn add(&self, other: &Q8) -> Q8 { | |
Q8 { value: self.value + other.value } | |
} | |
} | |
Q!(i8, 8, Q8) | |
fn main() { | |
let a = Q8 { value: 4 }; | |
let b = Q8 { value: 8 }; | |
io::println(fmt!("Q8: %?", a + b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment