Macro reuse parameter

Hello,

I can't find a way to reuse parameters from a macro, something like this:

macro_rules! test {
    ($test_name:ident,$func:ident,$exp:expr,$($value:expr),*) => {
        #[test]
        fn $test_name() {
            let exp=$exp;

            $func(S {
                variables: {
                    let variables=vec![$($value),*];
                    variables
                },exp:Some(exp)
            });
        }
    };
}

test!(test_fn,func_name,exp0=Something{a:0},exp0,Something{a:1},...);

I want to reuse exp0 inside "parameters" scope when I call test! pseudo code: is it possible ?.

Thank you in advance for your help.

I don't really understand what you're trying to do, but couldn't you capture exp0 as an extra ident fragment?

Yes , I could copy-paste exp0 multiple times but hat is not very clever and my expression is very long:

test!(test_fn,func_name,Something{a:0},Something{a:0},Something{a:1},...);

I would prefer:

test!({let exp0=Something{a:0};(test_fn,func_name,exp0,exp0,Something{a:1},...)});

or

{
let exp0=Something{a:0};
test!(test_fn,func_name,exp0,exp0,Something{a:1},...);
}

Can you give an example of what the expanded code should look like, and what should be the corresponding values of macro parameters? At the moment I don't understand what is your expected result.

I think it isn't possible to reuse parameters from macro rules.
I created a procedural function like macro to bypass the problem...
Thank you for your assistance.

Just for fun I tried writing declarative macro based on your first preference. I couldn't think of a way to support arbitrary many 'template' expressions though. Playground

Great workaround.
You use the value instead of the variable or the complete expression :+1:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.