gpt4 book ai didi

julia - 引用 Julia 中的所有参数

转载 作者:行者123 更新时间:2023-12-01 09:41:38 24 4
gpt4 key购买 nike

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

让这样的代码更短:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com