Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rust macro accept call to member function

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)*
        }
    }};
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading