Awesome, that works. Thank you! Is there some master list of these macro-tricks? An IPython-type worksheet might serve as a nice look-up table for how to get things done with macros.
On Sunday, August 3, 2014 3:04:53 PM UTC-4, Tim Holy wrote: > > 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! > >