Home Explore Blog CI



neovim

11th chunk of `runtime/doc/luaref.txt`
523383fd60ebf70757037ef3df3eaa58329c39ca675d09930000000100000fa0
 Operators                                      *lua-logicalop*

The logical operators in Lua are
>
       and    or    not
<
Like the control structures (see |lua-control|), all logical operators
consider both `false` and `nil` as false and anything else as true.

                                                  *lua-not* *lua-and* *lua-or*
The negation operator `not` always returns `false` or `true`. The conjunction
operator `and` returns its first argument if this value is `false` or `nil`;
otherwise, `and` returns its second argument. The disjunction
operator `or` returns its first argument if this value is different
from `nil` and `false`; otherwise, `or` returns its second argument.
Both `and` and `or` use short-cut evaluation, that is, the second operand is
evaluated only if necessary. Here are some examples:
>
       10 or 20            --> 10
       10 or error()       --> 10
       nil or "a"          --> "a"
       nil and 10          --> nil
       false and error()   --> false
       false and nil       --> false
       false or nil        --> nil
       10 and 20           --> 20
<
(In this manual, `-->` indicates the result of the preceding expression.)

------------------------------------------------------------------------------
2.5.4  Concatenation                                         *lua-concat*

The string concatenation operator in Lua is denoted by two dots (`..`).
If both operands are strings or numbers, then they are converted to strings
according to the rules mentioned in |lua-coercion|. Otherwise, the
"concat" metamethod is called (see |lua-metatable|).

------------------------------------------------------------------------------
2.5.5  The Length Operator                                   *lua-#* *lua-length*

The length operator is denoted by the unary operator `#`. The length of a
string is its number of bytes (that is, the usual meaning of string length
when each character is one byte).

The length of a table `t` is defined to be any integer index `n` such that `t[n]` is
not `nil` and `t[n+1]` is `nil`; moreover, if `t[1]` is `nil`, `n` may be zero. For a
regular array, with non-nil values from 1 to a given `n`, its length is exactly
that `n`, the index of its last value. If the array has "holes" (that
is, `nil` values between other non-nil values), then `#t` may be any of the
indices that directly precedes a `nil` value (that is, it may consider any
such `nil` value as the end of the array).

------------------------------------------------------------------------------
2.5.6  Precedence                                              *lua-precedence*

Operator precedence in Lua follows the table below, from lower to higher
priority:
>
       or
       and
       <     >     <=    >=    ~=    ==
       ..
       +     -
       *     /
       not   #     - (unary)
       ^
<
As usual, you can use parentheses to change the precedences in an expression.
The concatenation (`..`) and exponentiation (`^`) operators 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

Title: Lua Operators: Logical, Concatenation, Length, Precedence and Table Constructors
Summary
This section covers Lua's logical operators (and, or, not) and their behavior with `false` and `nil`, including short-circuit evaluation with examples. It details the string concatenation operator (`..`), and the length operator (`#`) for strings and tables, explaining how it determines the length of arrays with potential 'holes'. It also shows the operator precedence in Lua, from lowest to highest, and discusses the associativity of the concatenation and exponentiation operators. Finally, it covers table constructors and the different ways to initialize table fields.