Skip to content

Instantly share code, notes, and snippets.

@auroranockert
Last active December 11, 2015 18:58
Show Gist options
  • Save auroranockert/4644880 to your computer and use it in GitHub Desktop.
Save auroranockert/4644880 to your computer and use it in GitHub Desktop.
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)))
}
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))
}
test.rs:67:31: 67:36 error: binary operation + cannot be applied to type `Q8`
test.rs:67 io::println(fmt!("Q8: %?", a + b))
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