5

I'm trying to implement a macro which implements the Add trait for a struct, like so:

macro_rules! implement_add {
    ($t:ty) => {
        impl std::ops::Add for $t {
            type Output = $t;
            fn add(self, rhs: $t) -> $t {
                $t(self.0 + rhs.0)        // error on this line
            }
        }
    }
}

pub struct Length(f64);

implement_add!(Length);

fn main() {}

However, this gives an error on the indicated line:

<anon>:6:17: 6:19 error: unexpected token: `Length`
<anon>:6                 $t(self.0 + rhs.0)        // error on this line
                         ^~

This makes no sense to me. Especially since, if I replace $t there with Length, it compiles fine. Am I doing something wrong in my macro?

Playground: http://is.gd/EIEKub

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274

1 Answers1

4

You've stumbled on a subtle bit of Rust's type system. Length is a type, but Length() is a function. These exist in different namespaces.

One work around is to extend your macro to accept a type and a function:

macro_rules! implement_add {
    ($t:ty, $c:ident) => {
        impl std::ops::Add for $t {
            type Output = $t;
            fn add(self, rhs: $t) -> $t {
                $c(self.0 + rhs.0)        // error on this line
            }
        }
    }
}

pub struct Length(f64);

implement_add!(Length, Length);

fn main() {}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • @BenjaminLindley FWIW, you should probably do `ret.0 = ret.0 + rhs.0;` instead, since `+=` isn't generic yet, [so arbitrary addables fail](http://is.gd/ApfWLf). – Veedrac Aug 11 '15 at 10:40