EDIT: Oops posted this just after Cameron's answer. I'll leave it here in case it is helpful, but honestly, it says pretty much the same thing as his answer :-)
In Julia, the generic function signature is f(args ; kwargs)
. The names of the regular input arguments args
don't really matter, it is the order in which they appear in the function signature that is important. In contrast, for the keyword arguments kwargs
it the name that matters, and the order is not important.
The final piece of information we need is that for either args
or kwargs
, the splatting operator ...
separates out the elements of the container being splatted into individual arguments for the function signature.
So, let's apply this all to your example. Consider the following two functions:
function f1(foo::Int, bar::String)::String
return "$(foo) --> $(bar)"
end
function f2( ; foo::Int=0, bar::String="nil")::String
return "$(foo) --> $(bar)"
end
The first function only has args
while the second function only has kwargs
. In the case of the first function, names don't matter since we're dealing with args
, so in order to splat a container we would use just use a vector or tuple, e.g.:
params = [1, "baz"]
f1(params...)
The second function only has kwargs
, and Julia expects to see these expressed as the type Pair{Symbol,T}
for some T<:Any
. So, if we construct our dictionary to map the name of the keyword argument (expressed as a Symbol
) to the value, then the splatting operator will splat each entry of the dictionary into the function signature like so:
params = Dict(:foo=>1, :bar=>"baz")
f2(; params...)
Note that I had to use ;
in the function call to indicate there were no args
, only kwargs
.
And of course, if needed, you can have both at the same time, e.g. f3(vectorofargs... ; dictofargs...)
.