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