Create a struct on the fly with given name and attributes?

Hi,

Is it possible to create a struct with given name and its attribute name with types. In my application,
in each example I create different structs as needed for the physics. Each struct may differ by
some attributes. And these are operated by a macro. So macro will work on its attributes.

To summarize, Can I have some thing like

let rigd_fluid = get_particle_array!(name="rigidbody", x=x, y=y, z=z, m=m, pressure=p, rho=rho)

Or create the struct attribute with default types?

Thank you.

The order of your named arguments will have to be the same every time, but you can do something like this:

macro_rules! get_particle_array {
    ($value:expr, x=$x:tt, y=$y:tt) => {
        println!("{} {}", $value.$x, $value.$y);
    };
    ($value:expr) => {
        get_particle_array!($value, x=x, y=y)
    };
}

playground

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.