julia - 引用 Julia 中的所有参数

标签 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

(理论上)较短的版本:

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/

相关文章:

twitter - 读取包含 Twitter 表情符号描述的文件

julia - 我可以使用 conda 和 How 创建 julia 1.4.2 的虚拟环境吗

julia - 如何使用下一个有效观察将 `missings` 填充到向量中

Julia 1.1.1 - 绝对全局变量

Julia 与函数 - NoMethodError

julia - 在 Julia 中初始化一个空的元组数组

julia - 将 lambda 向量传递给 Poisson(),或惯用函数组合指南

function - Julia 中的卡方表函数

julia - 获取函数签名

python - 与 Julia 稀疏矩阵函数中的 to_scipy_sparse_matrix 类似的函数