在julia的dict中收集函数关键字参数

时间:2017-10-26 07:43:14

标签: julia keyword-argument

如何在Julia的dict中获取关键字参数及其函数的默认值?

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

2 个答案:

答案 0 :(得分:4)

您可以使用splat ...运算符:

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

或者如果您只想传递它:

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

答案 1 :(得分:1)

只需像这样创建一个Dict:

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