I am trying to recreate the function fun1
using metaprogramming, and fun2
is my attempt but I’m running into errors. How can I do this (or is there a better way to get this done)?
# Arbitrary struct
struct ABC
A
B
C
end
# The function I'm trying to create with meta programming
function fun1(ABC;
A = ABC.A,
B = ABC.B,
C = ABC.C,
)
return A + B + C
end
# The non-working version
keywords = join(["$field = ABC.$field" for field in fieldnames(ABC)], ",\n")
eval(quote
function fun2(ABC;
$(keywords),
)
return A + B + C
end
end)
The errors are
ERROR: syntax: invalid keyword argument syntax ""A = ABC.A,
B = ABC.B,
C = ABC.C"" around REPL[83]:2
Stacktrace:
[1] top-level scope
@ none:1
[2] eval
@ ./boot.jl:373 [inlined]
[3] eval(x::Expr)
@ Base.MainInclude ./client.jl:453
[4] top-level scope
@ REPL[83]:1