Is it possible to create a function with kwargs that returns a kwargs NamedTuple like this get_abc(;a,b,c) = (a=a,b=b,c=c)
without =(a=a,b=b,c=c)
? Or need macroses ?
Since Julia 1.6+ or so, you can do this:
julia> get_abc(;a,b,c) = (; a, b, c)
get_abc (generic function with 1 method)
julia> get_abc(a=1, b=2, c=3)
(a = 1, b = 2, c = 3)
or this:
julia> get_kws(; kwargs...) = (; kwargs...)
get_kws (generic function with 1 method)
julia> get_kws(; a = 1, b = 2, c = 3)
(a = 1, b = 2, c = 3)
3 Likes
macro kwargs_return_function(f::Symbol, args...)
_f = Symbol(:_,f)
return :($(esc(
quote
$_f(; $(args...)) = nothing
$f(; kwargs...) = ($_f(; kwargs...); (; kwargs...))
end
)))
end
better
macro kwret(f::Symbol, args...)
return :($(esc(
quote
$f(; $(args...)) = (; $(args...))
end
)))
end
these functions are needed to control the arguments