得到了一个带有三个参数的函数。
f(a, b, c) = # do stuff
还有另一个返回元组的函数。
g() = (1, 2, 3)
如何将元组作为函数参数传递?
f(g()) # ERROR
最佳答案
使用Nanashi的示例,提示是调用f(g())
时的错误
julia> g() = (1, 2, 3)
g (generic function with 1 method)
julia> f(a, b, c) = +(a, b, c)
f (generic function with 1 method)
julia> g()
(1,2,3)
julia> f(g())
ERROR: no method f((Int64,Int64,Int64))
这表明这将元组
(1, 2, 3)
作为f
的输入,而没有将其拆包。要打开包装,请使用省略号。julia> f(g()...)
6
Julia手册中的相关部分位于:http://julia.readthedocs.org/en/latest/manual/functions/#varargs-functions
关于julia - 如何将元组作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873478/