-- f() is adjusted to 3 results
return f() -- returns all results from f()
return ... -- returns all received vararg parameters
return x,y,f() -- returns x, y, and all results from f()
{f()} -- creates a list with all results from f()
{...} -- creates a list with all vararg parameters
{f(), nil} -- f() is adjusted to 1 result
<
An expression enclosed in parentheses always results in only one value. Thus,
`(f(x,y,z))` is always a single value, even if `f` returns several values.
(The value of `(f(x,y,z))` is the first value returned by `f` or `nil` if `f` does not
return any values.)
------------------------------------------------------------------------------
2.5.1 Arithmetic Operators *lua-arithmetic*
Lua supports the usual arithmetic operators: the binary `+` (addition),
`-` (subtraction), `*` (multiplication), `/` (division), `%` (modulo)
and `^` (exponentiation); and unary `-` (negation). If the operands are numbers,
or strings that can be converted to numbers (see |lua-coercion|), then all
operations have the usual meaning. Exponentiation works for any exponent. For
instance, `x^(-0.5)` computes the inverse of the square root of `x`. Modulo is
defined as
>lua
a % b == a - math.floor(a/b)*b
<
That is, it is the remainder of a division that rounds the quotient towards
minus infinity.
------------------------------------------------------------------------------
2.5.2 Relational Operators *lua-relational*
The relational operators in Lua are
>
== ~= < > <= >=
<
These operators always result in `false` or `true`.
Equality (`==`) first compares the type of its operands. If the types are
different, then the result is `false`. Otherwise, the values of the operands
are compared. Numbers and strings are compared in the usual way. Objects
(tables, userdata, threads, and functions) are compared by reference: two
objects are considered equal only if they are the same object. Every time you
create a new object (a table, userdata, or function), this new object is
different from any previously existing object.
You can change the way that Lua compares tables and userdata using the "eq"
metamethod (see |lua-metatable|).
The conversion rules of coercion |lua-coercion| do not apply to
equality comparisons. Thus, `"0"==0` evaluates to `false`, and `t[0]` and
`t["0"]` denote different entries in a table.
The operator `~=` is exactly the negation of equality (`==`).
The order operators work as follows. If both arguments are numbers, then they
are compared as such. Otherwise, if both arguments are strings, then their
values are compared according to the current locale. Otherwise, Lua tries to
call the "lt" or the "le" metamethod (see |lua-metatable|).
------------------------------------------------------------------------------
2.5.3 Logical 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