Suppose I just want to do something like f(x, ind) = x[ind] = 5.0
, I could write a similar macro like this:
julia> macro foo1(data, index)
esc(quote
$data[$index] = 5.0
end)
end
@foo1 (macro with 1 method)
julia> d1 = zeros(10);
julia> @foo1(d1, 3)
5.0
julia> d1[3] == 5
true
However, if I want some intermediate variable involved in it, like:
function foo2(data, index)
newindex = index * 2 # or other types of manipulation
data[newindex] = 5.0
end
I try to write the following:
julia> macro foo2(data, index)
newindex = gensym(:newindex)
esc(quote
$(newindex) = $(index) * 2
quote
$$(data)[$$(newindex)] = 5.0
end
end)
end
@foo2 (macro with 1 method)
julia> d2 = zeros(10);
julia> @macroexpand @foo2(d2, 3)
quote
#= REPL[16]:4 =#
##newindex#365 = 3 * 2
#= REPL[16]:5 =#
(Core._expr)(:block, $(QuoteNode(:(#= REPL[16]:6 =#))), (Core._expr)(:(=), (Core._expr)(:ref, d2, ##newindex#365), 5.0))
end
Notice that the final expr has d2
in it instead of :d2
, which the later is what I want. This isn’t setting the d2
actually. How can I achieve this? I checked this post but it doesn’t deal with this situation.
Thanks!