let’s say you have a function (in the Main scope):
function foo()
for cur_index in 99:-1:0
print("$(cur_index) bottles of beer on the wall. ")
println("$(cur_index) bottles of beer.")
println("Take one down. Pass it around.")
sleep(0.5)
end
end
and you want to run another function at the same time through some macro buzz
(described later):
module Fizz
function bar()
cows_come_home = false
@async while !cows_come_home
cur_rand = rand(0:99)
cows_come_home = ( cur_rand < 5 )
println("$(cur_rand) bottles of beer!")
sleep(cur_rand/100)
end
end
# macro buzz defined below
# (just a wrapper for bar func)
export @buzz
end
if you wanted to create a macro, that:
- calls
bar
no matter what and - calls some expression
async
-ly
How would you do it?
The following macro gives an undefined error:
module Fizz
# function bar defined above
# (just wrapped by buzz)
macro buzz(cur_expr::Expr)
bar()
cur_expression = Expr(
:macrocall,
Symbol("@async"),
cur_expr
)
return cur_expression
end
export @buzz
end
when making the call:
julia> using Fizz
julia> @buzz foo()
20 bottles of beer!ERROR (unhandled task failure): UndefVarError: foo not defined
Stacktrace:
[1] (::##1#2)() at ./task.jl:335
Task (failed) @0x00000001246d3610
UndefVarError: foo not defined
(::##1#2)() at ./task.jl:335
julia> 74 bottles of beer!
2 bottles of beer!