> thanks. that worked.
>
> so my mistake was to use a trait instead of a type in the generic call?
instead of Add<VecRhs... it should be
> Add<R:VecRhs... right?
>
>
> impl Add<R:*VecRhs*, Vec> for Vec {
> fn add(&self, rhs: &R) -> Vec {
> rhs.add_to_Vec(self)
> }
> }
>
>
> so now I need to implement VecRhs for each type I need function
overloading for. if I want to implement it for every
> real, could I write something like this?
>
> impl<T:Real> VecRhs for T {
> fn add_to_Vec(&self, lhs: &Vec) -> Vec {
> Vec { x:lhs.x+ *self as float, y:lhs.y+*self as float }
> }
> }
>
>
> cheers,
>
> Rémi
Sadly I don't think we have a trait right now that captures all
floating-point types. You'll need to have one impl block for float, f32,
and f64. However, you can avoid code duplication here by using macros:
macro_rules! float_impl(
($foo:ty) => (
impl VecRhs for $foo {
fn add_to_Vec(&self, lhs: &Vec) -> Vec {
Vec { x:lhs.x+ *self as float, y:lhs.y+*self as float }
}
}
)
)
float_impl!(float)
float_impl!(f32)
float_impl!(f64)
The above will generate the necessary impl block for each type.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev