# Generating Structs at Runtime; Tips and Tricks?

**URL:** <https://discourse.julialang.org/t/generating-structs-at-runtime-tips-and-tricks/54096>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [January 28, 2021, 3:01am UTC](https://discourse.julialang.org/t/generating-structs-at-runtime-tips-and-tricks/54096 "2021-01-28T03:01:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![olynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olynch/32/17839_2.png) [@olynch](https://discourse.julialang.org/u/olynch)\
**Post date:** [January 28, 2021, 3:01am UTC](https://discourse.julialang.org/t/generating-structs-at-runtime-tips-and-tricks/54096/1 "2021-01-28T03:01:37Z")

</div>

Hello all,

For various reasons, I would like to write a macro that `eval`s code at runtime to construct a custom struct. The problem is that the code that I am `eval`ing is not hygenically generated, because it comes out of a function that’s running at runtime, and not a macro.

Essentially, the reason for this is that these custom structs need to “inherit” from each other.

The other way that I was considering doing this was by having the custom struct declare a “mixin” macro, containing all of the fields, and then be able to use the “mixin” macro inside later macros. Is it possible to write a macro that declares a macro, and then use that declared macro inside further macros?

This all sounds horribly janky, but I promise it’s for a good purpose.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [January 28, 2021, 3:30am UTC](https://discourse.julialang.org/t/generating-structs-at-runtime-tips-and-tricks/54096/2 "2021-01-28T03:30:57Z")

</div>

[Mixers.jl](https://github.com/rafaqz/Mixers.jl) is exactly that macro.

My first package. I thought I needed it then and it works pretty well, but I rarely use it now.

It’s usually better to just write out the fields again.

---

<div class="post-metadata">

**Author:** ![olynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olynch/32/17839_2.png) [@olynch](https://discourse.julialang.org/u/olynch)\
**Post date:** [January 28, 2021, 3:34am UTC](https://discourse.julialang.org/t/generating-structs-at-runtime-tips-and-tricks/54096/3 "2021-01-28T03:34:29Z")

</div>

Oh fantastic! The structs I’m generating are a little more complex than those in Mixers.jl, but a similar approach should work well, so I’m going to study the code.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [January 28, 2021, 3:35am UTC](https://discourse.julialang.org/t/generating-structs-at-runtime-tips-and-tricks/54096/4 "2021-01-28T03:35:25Z")

</div>

I mean those are just the examples, it works for anything. I used to use it with FieldMetadata.jl macros 5 columns deep with no hassle (but that’s a bad idea).

Its pasting the whole lines of ast into the destination, whatever it is - and adding the type parameters at the same time.
