I’m having a difficult time understanding how to properly use macros. I’m trying to make a macro
@passkwargs that will turn
foo(; @passkwargs(x,y)) into
foo(; x=x, y=y)
I’m just starting to work with macros and I figured this would be a useful and relatively easy. Unfortunately I’m having a really difficult time navigating variable scope.
I’ve focused around using the idea that you can pass a Pair(Symbol, Any) as a keyword argument. My first try was this:
macro passkwargs(args...) result = (Pair(i,i) for i in args) return :($(result)...) end
But instead of getting the desired result, it gives the symbols as both keywords and values. So I tried to evaluate the second item in the pair
macro passkwargs(args...) result = (Pair(i,eval(i)) for i in args) return :($(result)...) end
but of course it evaluates the variable in the global scope, and not the scope of the function for which it is called. I’ve since been trying to figure out how esc() works but for some reason its not clicking - I get the Symbol as the keyword but the Expr is the value.
How do I do this? Any help would be appreciated