1

I'm trying to replace a bunch of snippets like

if let Some(thing) = some_t {
    thing.doSomething();
}

with a macro, resulting in something like

if_some(some_t, doSomething());

so basically replacing

if_some(/* option */, /* member function call(s) */);

with

if let Some(thing) = /* option */ {
    thing./* member function call(s) */
}

The attempt below does however not seem to work (the compiler reports "error: unexpected token: doSomething()", which does not help me in understanding what is wrong here. Anyone has an idea on how to handle this?

#[macro_export]
macro_rules! if_some {
    ( $option:ident, $function:expr ) => {{
        if let Some(thing) = $option {
            thing.$function
        }
    }};
}

struct Thing {}

impl Thing {
    fn doSomething(&self) {}
}

fn main() {
    let some_t = Some(Thing {});

    if let Some(thing) = some_t {
        thing.doSomething();
    }

    if_some!(some_t, doSomething());
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
Jonas
  • 1,356
  • 2
  • 11
  • 16
  • This is basically the same mistake as in [this question](https://stackoverflow.com/questions/37006835/building-an-enum-inside-a-macro): `$function: expr` doesn't mean that `$function` looks like an expression, but it means that it _is_ literally a _complete_ expression and so can't be used as a subexpression in `thing.$function`. The solution is also the same as in the other question: use `$($function:tt)*` to capture an untyped token sequence. – Jmb Sep 05 '22 at 14:52

1 Answers1

2

Once doSomething() is captured as expr, it will always stay so. The compiler does not see thing.doSomething(), it sees thing.<expr> and this is invalid Rust. You cannot write an expression after the dot, it needs to be an identifier.

The easiest way to fix that is to instead capture it as list of tts. tts can be inspected after they are captured:

#[macro_export]
macro_rules! if_some {
    ( $option:ident, $($function:tt)* ) => {{
        if let Some(thing) = $option {
            thing.$($function)*
        }
    }};
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • makes sense, i thought the right hand side of the dot would be an expression as well... This seems to work, thank you! – Jonas Sep 05 '22 at 14:56