Home Explore Blog CI



neovim

12th chunk of `runtime/doc/luaref.txt`
612b624d1caf5bcd5f5e3673cdb61ce9153c6ec9f7b319c80000000100000fa2
 are right
associative. All other binary operators are left associative.

------------------------------------------------------------------------------
2.5.7  Table Constructors                                *lua-tableconstructor*

Table constructors are expressions that create tables. Every time a
constructor is evaluated, a new table is created. Constructors can be used to
create empty tables, or to create a table and initialize some of its fields.
The general syntax for constructors is
>
       tableconstructor ::= { [ fieldlist ] }
       fieldlist ::= field { fieldsep field } [ fieldsep ]
       field ::= [ exp ]  = exp | Name = exp | exp
       fieldsep ::=  , |  ;
<
Each field of the form `[exp1] = exp2` adds to the new table an entry with
key `exp1` and value `exp2`. A field of the form `name = exp` is equivalent to
`["name"] = exp`. Finally, fields of the form `exp` are equivalent to
`[i] = exp`, where `i` are consecutive numerical integers, starting with 1.
Fields in the other formats do not affect this counting. For example,
>lua
       a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
<
is equivalent to
>lua
       do
         local t = {}
         t[f(1)] = g
         t[1] = "x"         -- 1st exp
         t[2] = "y"         -- 2nd exp
         t.x = 1            -- temp["x"] = 1
         t[3] = f(x)        -- 3rd exp
         t[30] = 23
         t[4] = 45          -- 4th exp
         a = t
       end
<
If the last field in the list has the form `exp` and the expression is a
function call, then all values returned by the call enter the list
consecutively (see |lua-function|). To avoid this, enclose the function
call in parentheses (see |lua-expressions|).

The field list may have an optional trailing separator, as a convenience for
machine-generated code.

------------------------------------------------------------------------------
2.5.8  Function Calls                     *lua-function*

A function call in Lua has the following syntax:
>
       functioncall ::= prefixexp args
<
In a function call, first `prefixexp` and `args` are evaluated. If the value
of `prefixexp` has type `function`, then this function is called with the given
arguments. Otherwise, the `prefixexp` "call" metamethod is called, having as
first parameter the value of `prefixexp`, followed by the original call
arguments (see |lua-metatable|).

The form
>
       functioncall ::= prefixexp : Name args
<
can be used to call "methods". A call `v:name(` `args` `)` is syntactic sugar
for `v.name(v,` `args` `)`, except that `v` is evaluated only once.

Arguments have the following syntax:
>
       args ::=  ( [ explist1 ] )
       args ::= tableconstructor
       args ::= String
<
All argument expressions are evaluated before the call.  A call of the
form `f{` `fields` `}` is syntactic sugar for `f({` `fields` `})`, that is, the
argument list is a single new table. A call of the form `f'` `string` `'`
(or `f"` `string` `"` or `f[[` `string` `]]`) is syntactic sugar for
`f('` `string` `')`, that is, the argument list is a single literal string.

As an exception to the free-format syntax of Lua, you cannot put a line break
before the `(` in a function call. This restriction avoids some ambiguities
in the language. If you write
>lua
       a = f
       (g).x(a)
<
Lua would see that as a single statement, `a = f(g).x(a)`. So, if you want two
statements, you must add a semi-colon between them. If you actually want to
call `f`, you must remove the line break before `(g)`.

                                                               *lua-tailcall*
A call of the form `return` `functioncall` is called a tail call. Lua
implements proper tail calls (or proper tail recursion): in a tail call, the
called function reuses the stack entry of the calling function. Therefore,
there is no limit on the number of nested tail calls that a program can
execute. However, a tail call erases any debug information about the calling
function. Note that a tail call only

Title: Lua Table Constructors and Function Calls
Summary
This section describes table constructors in Lua, explaining how they are used to create and initialize tables, including various field assignment syntaxes like `[exp1] = exp2`, `name = exp`, and `exp`. It clarifies how consecutive numerical indices are assigned to fields without explicit keys and how function calls as the last field in a list can affect table initialization. The section also details function call syntax, method call syntax (`v:name(args)`), and how table constructors and strings can be used as arguments. Special cases like line breaks before parentheses in function calls are also mentioned. Lastly, it introduces tail calls and their optimization in Lua, where the called function reuses the stack entry of the calling function, enabling unlimited nested tail calls but erasing debug information.