I am writing a Julia package to implement a REST API, and I have the accepted parameters available as NamedTuple
constants. I’d like to have these NamedTuple
constants double as the default keyword arguments for a function. I’d rather not just write a generic kwargs...
in the definition, because it would be handy to have the user be able to tab-complete the acceptable parameters.
Is this possible, possibly with macro magic? I’d like the two definitions of query
below to be identical at runtime.
const PARAMETERS = (; a = 1, b = 2)
function query(; PARAMETERS...)
...
end
function query(; a = 1, b = 2)
...
end
I’ve tried the following, and I think I’m close, but for some reason the character "
is being interpolated into the expression.
julia> @eval f(; $(replace(string(p), "("=>"", ")"=>""))) = a + b
ERROR: syntax: invalid keyword argument syntax ""a = 1, b = 2""
Stacktrace:
[1] top-level scope
@ none:1
[2] eval(m::Module, e::Any)
@ Core ./boot.jl:370
[3] top-level scope
@ REPL[46]:1