Hello,
I’m trying to define a macro which will evaluate an expression using @sync @parallel
if the Boolean argument passed in is true. When I define this macro in the REPL, it seems to work fine, but it throws a mysterious MethodError
at compile-time when I define it within a module.
I define TestModule
in the file test_module.jl:
module TestModule
macro mypar(parallel::Bool, ex::Expr)
return :( $(esc(parallel)) ? (@sync @parallel $(esc(ex))) : $(esc(ex)) )
end
function myfun(parallel::Bool)
@mypar parallel for i = 1:10
sleep(rand())
@show i
end
end
end
Then I get:
julia> include("test_module.jl")
ERROR: LoadError: MethodError: no method matching @mypar(::Symbol, ::Expr)
Closest candidates are:
@mypar(::Bool, ::Expr) at /home/pearl/test_module.jl:4
Stacktrace:
[1] include_from_node1(::String) at ./loading.jl:569
[2] include(::String) at ./sysimg.jl:14
while loading /home/pearl/test_module.jl, in expression starting on line 7
However, when I change the first line of myfun
to @mypar true for i = 10
, it compiles successfully. What am I doing incorrectly here?