ksmet1977
ksmet1977

Reputation: 31

collect function keyword arguments in dict in julia

How can I get the keyword arguments and their default values of a function in a dict in Julia?

E.g:

function foo(x; a = 1, b = 2, c= 3)
  # How do I get a dict of keyword arguments: Dict(a=>1, b=>2,c=3) ??,
  # so I can pass this Dict easily to another generic function taking v 
  # variable keyword arguments for further processing
end

Upvotes: 0

Views: 1154

Answers (2)

xiaodai
xiaodai

Reputation: 16074

Just create a Dict like so:

function foo(x; a = 1, b = 2, c= 3)
  Dict(:a => a, :b => b, :c => c)
end

Upvotes: 1

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

You can use the splat ... operator:

function foo(x; kwargs...)
   Dict(kwargs)
end

or if you just want to pass it:

function foo(x; kwargs...)
   innerfunction(x; kwargs...)
end

Upvotes: 4

Related Questions