Home Explore Blog CI



neovim

14th chunk of `runtime/doc/luaref.txt`
fb0fd5ddf25d86716b0fabdacc1f6add02f4f826b7d44ad90000000100000fa2
 tables.

Parameters act as local variables that are initialized with the argument
values:
>
       parlist1 ::= namelist [ , ... ] | ...
<
                                                                 *lua-vararg*
When a function is called, the list of arguments is adjusted to the length of
the list of parameters, unless the function is a variadic or 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 the call is enclosed in parentheses).

As an example, consider the following definitions:
>lua
       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
       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)
       g(3, 4)          a=3, b=4,   ... -->  (nothing)
       g(3, 4, 5, 8)    a=3, b=4,   ... -->  5  8
       g(5, r())        a=5, b=1,   ... -->  2  3
<
Results are returned using the `return` statement (see |lua-control|).
If control reaches the end of a function without encountering
a `return` statement, then the function returns with no results.

                                                            *lua-colonsyntax*
The colon syntax is used for defining methods, that is, functions that have an
implicit extra parameter `self`. Thus, the statement

       `function t.a.b.c:f (`  `params`  `)`  `body`  `end`

is syntactic sugar for

       `t.a.b.c:f = function (`  `self`,  `params`  `)`  `body`  `end`

==============================================================================
2.6  Visibility Rules                                    *lua-visibility*

Lua is a lexically scoped language. The scope of variables begins at the first
statement after their declaration and lasts until the end of the innermost
block that includes the declaration. Consider the following example:
>lua
       x = 10                -- global variable
       do                    -- new block
         local x = x         -- new `x`, with value 10
         print(x)            --> 10
         x = x+1
         do                  -- another block
           local x = x+1     -- another `x`
           print(x)          --> 12
         end
         print(x)            --> 11
       end
       print(x)              --> 10  (the global one)
<
Notice that, in a declaration like `local x = x`, the new `x` being declared is
not in scope yet, and so the second `x` refers to the outside variable.

                                                                *lua-upvalue*
Because of the lexical scoping rules, local variables can be freely accessed
by functions defined inside their scope. A local variable used by an inner
function is called an upvalue, or external local variable, inside the inner
function.

Notice that each execution of a local statement defines new local variables.
Consider the following example:
>lua
       a = {}
       local x = 20
       for i=1,10 do
         local y = 0
         a[i] = function () y=y+1; return x+y end
       end
<
The loop creates ten closures (that is, ten instances of the anonymous
function). Each of these closures uses a different `y` variable, while all of
them share the same

Title: Lua Function Parameters, Varargs, Colon Syntax, and Visibility Rules
Summary
This section explains function parameter handling in Lua, including how arguments are adjusted to match parameters and how variadic functions collect extra arguments into a vararg expression (represented by `...`). It uses examples to illustrate the mapping of arguments to parameters, especially in vararg functions. Then, it briefly mentions how results are returned using the `return` statement. Furthermore, the colon syntax is described as syntactic sugar for defining methods, implicitly passing a `self` parameter. Finally, the section discusses Lua's lexical scoping rules, explaining how variable scope is determined and demonstrating how local variables can shadow global variables. It also introduces the concept of upvalues (external local variables) used by inner functions, noting that each execution of a local statement creates new local variables, leading to multiple closures using different instances of those variables.