Hello, I would like to do something like d[:, :, 1] = some_array
:
function foo(d, x)
d[:,:,1] = x
end
Then I could write the following macro:
macro gen_foo(index)
fun = gensym()
quote
function $fun(d, x)
d[:,:,$index] = x
end
end
end
newfoo = @gen_foo(2)
d = rand(3, 4, 5)
x = rand(3, 4)
newfoo(d, x)
d[:,:,2] == x # true
However, the number of :
is hard-coded here and I would like to generalize it that I could do d[:, :, ..., :] = some_array
given how many :
I need by trying this:
macro gen_foo2(ndims, index)
fun = gensym()
quote
function $fun(d, x)
eval(quote
Expr(
:(=),
Expr(:ref, $$d, [:(:) for _ in 1: $$ndims]..., $$index),
$$x,
)
end)
end
end
end # not work
I couldn’t make it quite work and not sure how to reference arguments in $fun
. Does anyone have a clue for me?
Thanks!