I’m trying to write a macro and found a problem that I fail to debug, since macroexpand
gives code that runs just fine.
MWE (on Julia 0.6.3):
after defining
function _b(iter_vars, ranges)
iter_exprs = [:($(esc(v)) = $(esc(r))) for (v,r) in zip(iter_vars,ranges)]
var_array_ex = Expr(:vect, (esc(v) for v in iter_vars)...)
quote
$(Expr(:comprehension, Expr(:generator,
quote
$var_array_ex .+ 1
end,
iter_exprs...
)))
end
end
macro _b(iters...)
iter_vars = [e.args[1] for e in iters]
ranges = [e.args[2] for e in iters]
_b(iter_vars, ranges)
end
running the macro _b
gives a warning
julia> @_b( i=1:1, j=1:1 )
WARNING: .+ is no longer a function object; use broadcast(+, ...) instead
Stacktrace:
[1] depwarn(::String, ::Symbol) at .\deprecated.jl:70
[2] (::Base.##719#720)(::Array{Int64,1}, ::Int64) at .\deprecated.jl:355
[3] macro expansion at .\REPL[9]:0 [inlined]
[4] (::##7#8)(::Tuple{Int64,Int64}) at .\REPL[9]:7
[5] collect(::Base.Generator{Base.Iterators.Prod2{UnitRange{Int64},UnitRange{Int64}},##7#8}) at .\array.jl:470
[6] eval(::Module, ::Any) at .\boot.jl:235
[7] eval_user_input(::Any, ::Base.REPL.REPLBackend) at .\REPL.jl:66
[8] macro expansion at .\REPL.jl:97 [inlined]
[9] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at .\event.jl:73
while loading no file, in expression starting on line 5
1×1 Array{Array{Int64,1},2}:
[2, 2]
However, running the result of macroexpand
“manually” yields no warning:
julia> @macroexpand @_b( i=1:1, j=1:1 )
quote # REPL[9], line 5:
[begin # REPL[9], line 7:
[i, j] .+ 1
end for i = 1:1, j = 1:1]
end
julia> [begin
[i,j] .+ 1
end for i=1:1, j=1:1]
1×1 Array{Array{Int64,1},2}:
[2, 2]
I can work around this by using a loop, but would like to understand what is happening here.