引用 Julia 中的所有参数

Ale*_*hin 3 julia

是否可以捕获所有参数并将其传递给另一个函数?

使这样的代码更短:

function send_binary(;
  binary_hash::String,
  binary::String
)::Any
  post_json(
    "http://localhost:$(port)/v1/binaries",
    json((binary_hash=binary_hash, binary=binary))
  )
end
Run Code Online (Sandbox Code Playgroud)

(理论)更短的版本:

function send_binary(;
  binary_hash::String,
  binary::String
)::Any
  post_json("http://localhost:$(port)/v1/binaries", json(arguments))
end
Run Code Online (Sandbox Code Playgroud)

Dav*_*ela 6

图示操作 捕捉参数的函数定义和插值参数的函数调用:

julia> bar(; a=0, b=0) = a, b
bar (generic function with 1 method)

julia> foo(; kwargs...) = bar(; kwargs...)
foo (generic function with 1 method)

julia> foo(; a=1, b=2)
(1, 2)

julia> foo()
(0, 0)

julia> foo(; b=1)
(0, 1)
Run Code Online (Sandbox Code Playgroud)

你的例子可以写成:

function send_binary(; kwargs...)
  return post_json("http://localhost:$(port)/v1/binaries", json(; kwargs....))
end
Run Code Online (Sandbox Code Playgroud)