I would love to be able to something like the following:
macro_rules! impl_a_method(
($obj:ident, $body:block) => (
fn a_method(foo: Foo, bar:
That's not possible due to macro hygiene. Any identifier introduced in the macro body is guaranteed to be different from any identifier at the macro call site. You have to provide all identifiers yourself, which somewhat defies the purpose of the macro:
impl_a_method!(MyType, (foo, bar, baz), {
MyType {
foo: foo.blah(),
bar: bar.bloo(),
baz: baz.floozy(),
}
})
This is done by this macro:
macro_rules! impl_a_method(
($obj:ty, ($_foo:ident, $_bar:ident, $_baz:ident), $body:expr) => (
fn a_method($_foo: Foo, $_bar: Bar, $_baz: Baz) -> $obj { $body }
)
)
The only thing you're really saving here is writing types of method parameters.