Julia 1.0.
Me and metaprogramming, I don’t know, I am real slow at this!
I want to write a macro that, given a function definition
function f(x,y)
do something
end
will generate
function f(store,x,y)
do something
do something with store
end
now “x,y” is an array of expressions, I do not see how to put that back into the expression I am generating. Here is my MNWE:
macro store(ex) # given a function, create the same function with the additional first argument "store"
local name = ex.args[1].args[1]
local arglist = ex.args[1].args[2:end] # NB: array of Expr
local body = ex.args[2]
out= quote
function $(esc(name))(store,$arglist) # almost! arg is an array, and that does not work!
$(esc(body))
# do something with "store" here, not relevant for MWE
end
end
return out
end
@store function f(x)
y = x
for i = 1:3
y = 3y
end
z = y+2
return 2z
end
store()=nothing
f(store,3)