如何将元组作为函数参数传递 [英] how to pass tuple as function arguments

查看:311
本文介绍了如何将元组作为函数参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个接受三个参数的函数.

Got a function that takes three arguments.

f(a, b, c) = # do stuff

另一个返回元组的函数.

And another function that returns a tuple.

g() = (1, 2, 3)

如何将元组作为函数参数传递?

How do I pass the tuple as function arguments?

f(g()) # ERROR

推荐答案

使用Nanashi的示例,提示是调用f(g())

Using Nanashi's example, the clue is the error when you call 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而不打开其包装.要打开包装,请使用省略号.

This indicates that this gives the tuple (1, 2, 3) as the input to f without unpacking it. To unpack it use an ellipsis.

julia> f(g()...)
6

Julia手册中的相关部分位于: http://julia.readthedocs. org/en/latest/manual/functions/#varargs-functions

The relevant section in the Julia manual is here: http://julia.readthedocs.org/en/latest/manual/functions/#varargs-functions

这篇关于如何将元组作为函数参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆