Generating Structs at Runtime; Tips and Tricks?

Hello all,

For various reasons, I would like to write a macro that evals code at runtime to construct a custom struct. The problem is that the code that I am evaling 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.

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.

1 Like

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.

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.