是否可以捕获所有参数并将其传递给另一个函数?
让这样的代码更短:
function send_binary(;
binary_hash::String,
binary::String
)::Any
post_json(
"http://localhost:$(port)/v1/binaries",
json((binary_hash=binary_hash, binary=binary))
)
end
(理论上)较短的版本:
function send_binary(;
binary_hash::String,
binary::String
)::Any
post_json("http://localhost:$(port)/v1/binaries", json(arguments))
end
最佳答案
splat operator captures arguments在函数定义和 interpolates arguments 中在函数调用中:
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)
你的例子可以写成:
function send_binary(; kwargs...)
return post_json("http://localhost:$(port)/v1/binaries", json(; kwargs....))
end
关于julia - 引用 Julia 中的所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668984/