Your Exprs are sad because they're being passed to `eval`. Exprs dream only of being spliced into the AST by a macro and compiled in context!
(I'm assuming that your actual need is more sophisticated than this. If so, you probably want to use a proper macro for what you're trying to do--see the metaprogramming section of the user manual http://julia.readthedocs.org/en/latest/manual/metaprogramming/, and pay particular attention to the role of `esc()`. If you provide a little more detail at a higher level on what you're trying to solve, there might be an even simpler solution.) Patrick On Wednesday, May 6, 2015 at 7:58:10 PM UTC-5, Evan Pu wrote: > > Hi, just a quick question on the scope/environment of an expression. > > I am using the Expr construct inside a function to create an expression, > and I would then want to use this expression in various different places. > Here's a short example > > First we have a simple function "funky" that takes in a function "ff", and > applies it to two symbolic arguments :x and :y > julia> function funky(ff::Function) > Expr(:call, ff, :x, :y) > end > funky (generic function with 1 method) > > > We define x and y to be 1 and 2 respectively > julia> x,y = 1,2 > (1,2) > > > No surprise here, when funky is passed with the function "+", it adds x > and y together > julia> eval(funky(+)) > 3 > > > However, now I want to use this expression with different bindings for x > and y, I attempt the following > julia> function funfun(x, y) > eval(funky(+)) > end > funfun (generic function with 1 method) > > > Which doesn't seem to work. Here the result is sitll 3, presumably it is > using the earlier bindings of x=1, y=2 > julia> funfun(4,5) > 3 > > I would really liked the last result to be 9. > > > So far it seems the expression uses static scoping and it's looking up the > binding for :x and :y in the frame generated by calling "funky", and not > finding it there, it looks up :x and :y in the > global frame, which is 1 and 2. > Is there a way to bind the symbols in an expression to different things? > is there like an "environment" dictionary where we can mess with, or is > there some clean way of doing it? > > Much much much appreciated!!! > --evan >