To be honest, I don’t fully understand what goes wrong here, but this way of
doing it does work:
macro bar(num)
ex = Expr(:(=), esc(Expr(:call, :foo, :x)), esc(num))
return ex
end
@bar 5
foo(1)
I suspect that, in your example, there’s an attempt to evaluate the
sub-expressions in the wrong scope. For example, this code shouldn’t (and
doesn’t) work:
macro bar(num)
ex = Expr(:(=), Expr(:call, :foo, :x), esc(num))
return ex
end
@bar 5
foo(1)
— John
On Jan 14, 2014, at 4:47 PM, Eric Davies <[email protected]> wrote:
> julia> macro bar(num)
> :(foo(x) = $num)
> end
>
> julia> @bar 5
> foo#27 (generic function with 1 method)
>
> julia> foo
> ERROR: foo not defined
>
> It appears that variable/function definitions in macros are mangled somehow.
> Is there any way to define a function or set a variable in a macro (s.t. that
> definition/assignment occurs in the calling scope)?