Julia code and struct generation

You use Expr to build up expressions piece by piece, or the higher-level equivalent of quote expressions. You don’t need macros either (those are for transforming user syntax into different expressions), only eval on the generated expression.

Never use strings for metaprogramming. Strings are brittle.

That being said, you should rarely need to use metaprogramming; it’s certainly not the first tool you should reach for. Other alternatives in your case include parameterized types with Nothing for unused fields, as @SteffenPL suggests above, or named tuples which are essentially anonymous struct types that can be generated as needed.

1 Like