LUA variable length parameters... three dots

This article is translated from the official LUA documentation

 

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).

When a function is called, unless it is a variadic function, the length of the parameters when the function is called corresponds one-to-one with the parameters of the function. Variable parameters are represented by three dots... , at the end of the parameter list. A variadic parameter does not have a corresponding parameter list. He will collect all the parameters into a variadic table (three dots). The value of this expression represents all parameter values. It is similar to the case where a function returns multiple return values. If a variadic expression is inside other expressions or in the middle of some expressions, it returns only one element eg: local cc,dd,ee = a,...,34. If the expression is at the end of the expression list, it does not reconcile the arguments unless a parenthesis is used.

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 -- if no parameter is passed for b, then b is nil
     f(3, 4) a=3, b=4 -- parameter one-to-one correspondence
     f(3, 4, 5) a=3, b=4 -- redundant parameters have no effect
     f(r(), 10) a=1, b=10 -- the variable parameter is in the middle, and the value of the expression value is the first variable parameter list element
     f(r()) a=1, b=2 -- variable parameters are at the end, unaffected
     
     g(3) a=3, b=nil, ... --> (nothing) --b parameters and ... parameters are not passed parameters, it is nil
     g(3, 4) a=3, b=4, ... --> (nothing) -- nil if no variable parameter is passed
     g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 --The variable parameter is the last one, then the parameter list is automatically collected to the variable parameter
     g(5, r()) a=5, b=1, ... --> 2 3 --The second parameter b can be passed as a variable parameter, then b is the first variable parameter element. The third parameter is a variadic parameter that takes the remaining parameters of the variadic parameter

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324894915&siteId=291194637