Here's a simple example of what I'm observing:
testmodule.jl
module testmodule
export @wrapexpr
# if I put foo here, everything works
# foo(x, y) = x*y
macro wrapexpr(expr)
quote
(x, y) -> $(expr)
end
end
end
and testscript.jl:
using testmodule
foo(x, y) = x*y
fn1 = @wrapexpr x+y
fn2 = @wrapexpr begin foo(x, y) end
println("fn1 = ", fn1(1, 2))
println("fn2 = ", fn2(1, 2))
with the output:
fn1 = 3
ERROR: foo not defined
in anonymous at /home/abraham/tmp/testmodule.jl:6
in include at boot.jl:244
in include_from_node1 at loading.jl:128
while loading /home/abraham/tmp/testscript.jl, in expression starting on
line 9
but if I define foo in the module (the commented out line), I get:
fn1 = 3
fn2 = 2
On Tuesday, June 3, 2014 9:59:25 PM UTC-4, Abe Schneider wrote:
>
> I have a macro that is defined in a module that creates a dictionary of
> anonymous functions. These anonymous functions wrap arbitrary code which I
> pass in. If I pass in simple pieces of code (e.g. just returning a simple
> value), my test script everything works as expected.
>
> However, if I make a function in my test script, which then gets wrapped
> in an anonymous function (e.g. I do a begin/end block), I get an error that
> the function is not defined. But, if I move the function out of my test
> script to my module, everything works.
>
> I assume what is happening is that when the anonymous function is being
> made, it sees only the namespace of the module. This almost makes sense to
> me, except that the macro is being called from my test script, so the
> namespace should also be in the script.
>
> Is this expected behavior? If so, are there any suggestions on how to get
> around this?
>
> I can provide code if it will be helpful (I thought in this case it might
> just make things more complicated). Also, I'm using Julia version
> 0.3.0-prerelease+3337.
>
> Thanks!
>