Benjamin Lindley
Benjamin Lindley

Reputation: 103751

Using a macro type argument inside a generated function

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

Upvotes: 5

Views: 430

Answers (1)

Shepmaster
Shepmaster

Reputation: 432139

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() {}

Upvotes: 4

Related Questions