It's not obvious. Try this:

macro mytest(fn::Symbol, ex::Expr)
     qex = Expr(:quote, ex)  # here's the magic incantation
     quote
       $(esc(fn))($qex)
     end
end

Works for symbols that you want to keep as symbols, too.

But your `foo` method is not defined for an expression, so your test case will 
throw an error.

--Tim

On Sunday, August 03, 2014 11:46:43 AM Abe Schneider wrote:
> I found one previous conversation related to this, but unfortunately the
> answer didn't work for me. Hopefully I'm not asking an obvious question.
> 
> Suppose I have some macro:
> macro mytest(fn::Symbol, ex::Expr)
>   quote
>     $(esc(fn))($ex)
>   end
> end
> 
> and I have some function that takes in the expression to operate on it:
> myfun(ex::Expr) = ...
> 
> the problem (which may be obvious) is that `$ex` gets evaluated with the
> macro, and I won't pass an `Expr` to `myfun`. A quick demonstration:
> 
> foo(x, y) = x+y
> println(expandmacro(:(@mytest foo 1+2)))
> 
> gives:
> begin  # .../mtestmod.jl, line 38:
>     foo(mtestmod.+(1,2))
> end
> 
> What I can't figure out is how to keep `ex` an expression that is passed to
> foo (at least without writing out Expr by hand). I've tried many
> combinations of syntax to try to preserve the Expr-ness of `ex`.
> 
> Thanks!

Reply via email to