I have a function collect extra keyword arguments using ...
, so it is like function f(args=0; kwargs...)
. I want to check if a keyword argument, let' say, a
, exists in kwargs
.
What I do probably is not an elegant way, I first create a Dict
to store the keywords and corresponding values kwargs_dict=[key=>value for (key, value) in kwargs]
, then I use haskey(kwargs_dict, :a)
to check if a
is a key in the dict. Then I get its value by kwargs_dict[:a]
.
function f(; kwargs...)
kwargs_dict = [key=>value for (key, value) in kwargs]
haskey(kwargs_dict, :a)
a_value = kwargs_dict[:a]
end
f(args=0, a=2)
> true
f(args=0)
> false
I wonder if there is better way to check if the keyword argument a
is in kwargs
and to get the value of the existed keyword argument.