# Julia code and struct generation

**URL:** <https://discourse.julialang.org/t/julia-code-and-struct-generation/119044>\
**Category:** General Usage\
**Tags:** macros, code-generation\
**Created:** [September 4, 2024, 4:11pm UTC](https://discourse.julialang.org/t/julia-code-and-struct-generation/119044 "2024-09-04T16:11:47Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2024, 4:55pm UTC](https://discourse.julialang.org/t/julia-code-and-struct-generation/119044/3 "2024-09-04T16:55:36Z")

</div>

> [@devel-chm](#):
>
> Meta::parse() only seems to work for complete expressions  
> How am I supposed to build up the result and from which pieces?

You use [`Expr`](https://docs.julialang.org/en/v1/devdocs/ast/#Expr-types) to build up expressions piece by piece, or the higher-level equivalent of [quote expressions](https://docs.julialang.org/en/v1/devdocs/ast/#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](https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022); 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](https://docs.julialang.org/en/v1/manual/functions/#Named-Tuples) which are essentially anonymous `struct` types that can be generated as needed.

---

_[View the full topic](https://discourse.julialang.org/t/julia-code-and-struct-generation/119044)._
