# Macro for generating struct

**URL:** <https://discourse.julialang.org/t/macro-for-generating-struct/57479>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [March 18, 2021, 2:48pm UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479 "2021-03-18T14:48:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [March 18, 2021, 2:48pm UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479/1 "2021-03-18T14:48:53Z")

</div>

I have written a macro that generates a new `struct`. It all works as intended when I test the macro from the REPL or from a script. Then of course, I want to call the macro in a function that does some cooking and returns a `DataType` defined by the macro. Kablooie, because of course, you can not define a `struct` inside a `function`.

So I reckon that I want the macro to generate some code, and evaluate it in the module to which the function calling the macro belongs.

Any thoughts on pulling this stunt? 😀

I am lazy and tryiiing to get away without a MWE. I really _do_ look for trouble sometimes…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 18, 2021, 2:57pm UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479/2 "2021-03-18T14:57:16Z")

</div>

It sounds like you actually want to use `eval()` or `@eval`, in which case you don’t need a macro at all.

```julia
julia> function make_type_named(name)
         @eval struct $name end
       end
make_type_named (generic function with 1 method)

julia> make_type_named(:hello)

julia> x = hello()
hello()

```

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [March 18, 2021, 3:41pm UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479/3 "2021-03-18T15:41:14Z")

</div>

aha!!! `eval` evaluates into the macro! 👍 As simple as that…

Thank you!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 18, 2021, 4:13pm UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479/4 "2021-03-18T16:13:42Z")

</div>

You may be tempted to combine `eval` and macros in some way (perhaps by `eval`ing a macro call or using `eval` inside a macro). Resist this temptation–it’s basically never necessary, since once you are using `eval` you no longer need a macro at all.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [March 19, 2021, 5:40am UTC](https://discourse.julialang.org/t/macro-for-generating-struct/57479/5 "2021-03-19T05:40:00Z")

</div>

I agree. I burnt myself on this before. One things that crops up is “[who’s interpolating](https://discourse.julialang.org/t/generated-inside-macro-whos-interpolating/46895/14)”.

Thanks Robin!
