本文翻译自 LUA官方文档

When a function is called, the list of arguments is adjusted to the length of the list of parameters, unless the function is a vararg function, which is indicated by three dots ('...') at the end of its parameter list. A vararg function does not adjust its argument list; instead, it collects all extra arguments and supplies them to the function through a vararg expression, which is also written as three dots. The value of this expression is a list of all actual extra arguments, similar to a function with multiple results. If a vararg expression is used inside another expression or in the middle of a list of expressions, then its return list is adjusted to one element. If the expression is used as the last element of a list of expressions, then no adjustment is made (unless that last expression is enclosed in parentheses).

当一个函数被调用,除非是一个可变参数函数,否则函数调用时的参数长度与函数的参数一一对应。可变参数用三个点表示 ... ,在参数列表最后。一个可变参数不会有一个对应参数列表。他会收集所有的参数放进一个变参表式里(三个点)。这个表达式的值表示所有参数值。它与函数返回多返回值的那种情况是相似的。如果一个变参表达式在其它表达式内部或者在一些表达式的中间,他只返回是一个元素例如:local cc,dd,ee = a,...,34 。如果这个表达式是在表达式列表的最后一个,他不会对参数进行调和除非用一个括号。

As an example, consider the following definitions:

     function f(a, b) end
function g(a, b, ...) end
function r() return 1,2,3 end

Then, we have the following mapping from arguments to parameters and to the vararg expression:

     CALL            PARAMETERS

     f(3)             a=3, b=nil   --没有为b传参数则b为nil
f(3, 4) a=3, b=4 --参数一一对应
f(3, 4, 5) a=3, b=4 --多余传参数无作用
f(r(), 10) a=1, b=10 --可变参数在中间,表达值的值则是第一个变参列表元素
f(r()) a=1, b=2 --可变参数在最后面,不受影响 g(3) a=3, b=nil, ... --> (nothing) --b参数和...参数都没有传参数则为nil
g(3, 4) a=3, b=4, ... --> (nothing) --没有为可变参数传参则为nil
g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 --可变参数为最后一个,则自动收集参数列表到可变参数
g(5, r()) a=5, b=1, ... --> 2 3 --第二个参数b可传的是一个可变参,则b为可变参的第一个元素。第三个参数是个可变参他取可变参的剩余参数

LUA可变长参数 ... 三个点的更多相关文章

  1. 【lua】可变长参数

    lua可变长参数 在lua中可以使用...表示可变长参数,在函数内通过表访问可变参数 function rest(...) -- 把可变参数放在表类 local args = { ... } prin ...

  2. 【小白学Lua】之Lua变长参数和unpack函数

    一.简介 Lua的变长参数和unpack函数在实际的开发中应用的还挺多的,比如在设计print函数的时候,需要支持对多个变量进行打印输出,这时我们就需要用到Lua中的变长参数和unpack函数了. 二 ...

  3. Java中的可变长参数

    可变长参数的定义 与一般方法没多大差别,只不过形参多了...(三个点) 方法名(数据类型 ... 变量名){} 小案例: public class ParamDemo { public static ...

  4. javascript arguments解释,实现可变长参数。

    在C#中,有可变长参数params[],但是在js中,如何实现这种可变参数呢? 一.可变长参数 arguments是非常好的解决方法,一直不知道javascript有这个东西. 先来看看应用场景,使用 ...

  5. 方法的可变长参数 传入参数个数不确定可用(Type ... values)

    /** * 可变长的参数. * 有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法: * 1. 重载,多重载几个方法,尽可能的满足参数的个数.显然这不是什么好办法. ...

  6. Noah的学习笔记之Python篇:函数“可变长参数”

    Noah的学习笔记之Python篇: 1.装饰器 2.函数“可变长参数” 3.命令行解析 注:本文全原创,作者:Noah Zhang  (http://www.cnblogs.com/noahzn/) ...

  7. [转]深度探索C语言函数可变长参数

    转自:http://www.cnblogs.com/chinazhangjie/archive/2012/08/18/2645475.html 一.基础部分 1.1 什么是可变长参数 可变长参数:顾名 ...

  8. go实例—函数或方法的可变长参数

    支持可变长参数列表的函数可以支持任意个传入参数,比如fmt.Println函数就是一个支持可变长参数列表的函数. 需要注意的是,可变长参数应该是函数定义的最右边的参数,即最后一个参数 package ...

  9. Java 可变长参数列表

    Java中定义了变长参数,允许在调用方法时传入不定长度的参数. 定义及调用 在定义方法时,在最后一个形参后加上三点 …,就表示该形参可以接受多个参数值,多个参数值被当成数组传入.上述定义有几个要点需要 ...

随机推荐

  1. ajax hash缓存

    hash 模拟url路由 function hashdone(){        var hash;        hash=(!window.location.hash)?"#one&qu ...

  2. 【R】函数-概率函数

  3. SQL 连接操作 及 查询分析

  4. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十三)kafka+spark streaming打包好的程序提交时提示虚拟内存不足(Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical memory used; 2.2 GB of 2.1 G)

    异常问题:Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical mem ...

  5. Eclipse小技巧:收起outline的头文件

  6. GitHub万星项目:黑客成长技术清单

    最近有个GitHub项目很火,叫"Awesome Hacking",这个项目是由Twitter账号@HackwithGithub 维护,喜欢逛Twitter的安全爱好者应该了解,在 ...

  7. Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service

    用过Spring MVC的人都知道,我们如何在Controller中注入Service,可以使用@Resource注解的方法. 有时候,实际在项目的过程中,我们需要在某个Servlet中使用Servi ...

  8. PD 之 连接数据库并导出数据及生成PDM文件

    使用PowerDesigner工具,连接数据库并导出数据及生成PDM文件. 1.建立连接 “以管理员身份运行”打开PowerDesigner,右键“Workspace”→“New”→“Physical ...

  9. IPC's epoch 6 is less than the last promised epoch 7

    一.错误起因 Active NameNode日志出现异常IPC‘s epoch [X] is less than the last promised epoch [X+1],出现短期的双Active ...

  10. Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL

    1:Winform应用通过mshtml操作IE浏览器DOM时,第一次运行正常,点击第二次时错误信息如下 A first chance exception of type 'System.Runtime ...