Accessing a list of a function's keyword arguments?


#1

Can code inside function access a list of ll its keyword arguments?

I am writing a function with multiple keyword arguments, and I want to check the type of each of the arguments. An example might be something like

function tester(; a=1, b=2, c=3)
    if typeof(a)<:Float64 || typeof(b)<:Float64 || typeof(c)<:Float64
        @printf("I found a Float64\n")
    end
    # Then other code following after that
end

But that approach means that if I add a new keyword argument to the argument list, I also have to remember to add it to the set of checked arguments. What I would really like to do is have some way to access all the keyword arguments, as in

function tester(; a=1, b=2, c=3)
    my_kw_args = SOME_METHOD_THAT_RETURNS_THIS_FUNCTIONS_KW_ARGS()

    if any(map(k -> typeof(my_kw_args[k])==Float64, keys(my_kw_args)))
        @printf("I found a Float64\n")
    end
    # Then other code following after that
end

And this why I am asking: is there some way, inside a function, to access a list of all of the function’s keyword arguments?

Thanks in advance for any help or suggestions!!

(p.s. I know I could instead write my function as

function tester(; kwargs...)

but that is itself inconvenient. Having the values already assigned to variables, as with the usual a=default formulation, is really great.)


#2

A common solution is to define a structure that contains the relevant information, and pass that around. See eg Options(...) in Optim.jl.