I have a macro that takes a type as input and enhances it with a keyword constructor.
struct Foo a;z end @macroexpand @keyword_constructor Foo
:(Foo(; a=nothing, z=nothing) = begin # In, 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?
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