I think that this may be what you're looking for:
macro myprintln(exs...)
:(println($(map(esc,exs)...)))
end
Keep in mind that exs is just a tuple of values which happen to be symbols,
expressions or literals. Likewise, esc is just a function that you want to
apply to each of them and collect the result. Since map is how you would
typically do that, that's a good way to do this too.
On Mon, Sep 1, 2014 at 5:42 PM, Linpeng Tang <[email protected]> wrote:
> Hi, I am trying to write a macro that takes in varargs and escape them,
> but I haven't been able to do so with `esc`
>
> To illustrate, I want this (dummy) macro to println the variables passed
> in
>
> ```
> macro myprintln(exs...)
> :(println($(esc(exs...))))
> end
> ```
>
> However, this results in
> ```
> ERROR: `esc` has no method matching esc(::Expr, ::Expr)
> ```
>
> I tried to put `...` outside esc to `:(println($(esc(exs)...)))`, but
> when I do `macroexpand`, I get
>
> ```
> $(Expr(:error, MethodError(start,(:($(Expr(:escape, (:(a + 2),:(a +
> 3))))),))))
> ```
>
> I tried again, further promoting `...` to `:(println($(esc(exs))...))`
>
> But now when I do `macroexpand(:(@myprint(a+1, a+2)))`, I get
>
> ```
> println((:(a + 2),:(a + 3))...)
> ```
>
> And it doesn't work either.
>
> Any suggestions?
>