# Using eval inside macro?

**URL:** <https://discourse.julialang.org/t/using-eval-inside-macro/8183>\
**Category:** General Usage\
**Created:** [January 5, 2018, 3:29pm UTC](https://discourse.julialang.org/t/using-eval-inside-macro/8183 "2018-01-05T15:29:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 5, 2018, 3:29pm UTC](https://discourse.julialang.org/t/using-eval-inside-macro/8183/1 "2018-01-05T15:29:19Z")

</div>

I have a macro that takes a type as input and enhances it with a keyword constructor.

```julia
struct Foo a;z end
@macroexpand @keyword_constructor Foo

```

```julia
:(Foo(; a=nothing, z=nothing) = begin # In[2], line 9:
            Foo(a, z)
        end)

```

This relies on using `eval` inside the macro and I vaguely remember that using eval inside a macro is considered evil.

- Why is eval inside a macro a bad idea in general?
- Is it okay in this particular case?

```julia
macro keyword_constructor(T)
    M = current_module()
    fields = eval(M, :(fieldnames($T)))
    kws = map(field -> Expr(:kw, field, :nothing), fields)
    esc(:($T(;$(kws...)) = $T($(fields...))))
end

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 5, 2018, 3:39pm UTC](https://discourse.julialang.org/t/using-eval-inside-macro/8183/2 "2018-01-05T15:39:03Z")

</div>

`eval` in macro can easily evaluate in the wrong scope. It is totally OK if the intended sematic is to evaluate the part of the expression only in top-level (global) scope, which includes macros that should only be used in global scope, like `@enum`. You should also make sure the eval is done in the right module (`current_module()` pre-0.7 and ` __module__ ` post-0.7).

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 5, 2018, 3:41pm UTC](https://discourse.julialang.org/t/using-eval-inside-macro/8183/3 "2018-01-05T15:41:51Z")

</div>

I see thanks!

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 5, 2018, 11:56pm UTC](https://discourse.julialang.org/t/using-eval-inside-macro/8183/4 "2018-01-05T23:56:16Z")

</div>

You might also be interested in the currently non-exported [`@kwdef` macro](https://github.com/JuliaLang/julia/blob/fb527308bb134d7739eb01ea76ddc3d16109675f/base/util.jl#L566-L590)

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 6, 2018, 12:34pm UTC](https://discourse.julialang.org/t/using-eval-inside-macro/8183/5 "2018-01-06T12:34:25Z")

</div>

Thanks I was not aware of that! There is also [QuickTypes.jl](https://github.com/cstjean/QuickTypes.jl), which I find useful. The advantage of the above approach is that the typedef itself does not need to be decorated, making it easier to combine with other typedef tweaks.
