Operators
The following table lists the operators in descending order of evaluation, i.e. higher in the table means higher evaluation precedence. Multiple operators on the same line share evaluation precedence. Parenthesize an expression if you need to force an evaluation order.
| Operator(s) |
Description |
^ |
Exponentiation (raises a base to a power). Right-to-left associativity |
not, #, - |
Unary operators: Logical negation, length of strings/tables, and unary minus (negating a number).
|
*, /, //, % |
Multiplicative operators: Multiplication, division, floor division, and modulo (remainder). |
+, - |
Additive operators: Addition and subtraction. |
.. |
String concatenation (joins two strings together). |
==, ~=, <, >, <=,
>= |
Relational operators: Equal to, not equal to, less than, greater than, less than or equal to, and
greater than or equal to. |
and |
Logical conjunction (AND). Returns its first argument if it is false or nil; otherwise, returns its
second argument. |
or |
Logical disjunction (OR). Returns its first argument if it is neither false nor nil; otherwise,
returns its second argument. |
number type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
+, -, * |
number |
number |
number |
Standard addition, subtraction, and multiplication. |
/ |
number |
number |
number |
Standard float division. Always outputs a float. |
// |
number |
number |
number |
Floor division (division rounded down to the nearest integer). |
% |
number |
number |
number |
Modulo (returns the remainder of division). |
^ |
number |
number |
number |
Exponentiation (raises LHS to the power of RHS). |
- (Unary) |
number |
N/A |
number |
Negates the number. |
==, ~= |
number |
any |
boolean |
Checks for numeric equality or inequality. Comparison with non-numbers yields not equal. |
<, >, <=,
>= |
number |
number |
boolean |
Performs standard numeric order comparisons. |
.. |
number |
string or number |
string |
Converts the number to a string and concatenates it. |
and, or |
number |
any |
any |
Evaluates logical connections. Numbers are always treated as "truthy". Unlike LSL, the number 0 evaluates to true in SLua. |
not |
number |
N/A |
boolean |
Always returns false because numbers are always truthy (including 0). |
string type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
.. |
string |
string or number |
string |
Concatenates the two operands together. |
# (Unary) |
string |
N/A |
number |
Returns the size of the string in bytes. |
==, ~= |
string |
any |
boolean |
Compares string values. SLua's string interning optimizes this to a memory reference comparison. |
<, >, <=,
>= |
string |
string |
boolean |
Alphabetical (lexicographical) comparison based on internal character byte codes. |
and, or |
string |
any |
any |
Logical connections. Strings are always treated as truthy. |
not |
string |
N/A |
boolean |
Always returns false because strings are always truthy. |
boolean type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
and |
boolean |
any |
any |
Returns LHS if false; otherwise returns RHS. |
or |
boolean |
any |
any |
Returns LHS if true; otherwise returns RHS. |
not |
boolean |
N/A |
boolean |
Inverts the boolean's truth value. |
==, ~= |
boolean |
any |
boolean |
Evaluates identity. Yields true only if compared value is a boolean of identical state. |
vector type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
+, - |
vector |
vector |
vector |
Performs component-wise addition or subtraction. |
*, /, // |
vector |
vector |
vector |
Performs component-wise math operation between the two vectors. In SLua, * does not calculate Dot Product. Use vector.dot(a, b) instead. |
% |
vector |
vector |
vector |
Calculates the Cross Product of the two vectors. |
*, / |
vector |
number |
vector |
Scales (multiplies/divides) every vector component by the number. |
* |
number |
vector |
vector |
Commutative scaling: scales the vector components by the number. |
- (Unary) |
vector |
N/A |
vector |
Inverts the sign of all components of the vector. |
==, ~= |
vector |
any |
boolean |
Checks for exact component-wise equality. Comparing against other types yields not equal. |
and, or |
vector |
any |
any |
Logical connections. Vectors are always treated as truthy. |
not |
vector |
N/A |
boolean |
Always returns false because vectors are always truthy. |
rotation / quaternion type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
* |
rotation |
rotation |
rotation |
Composition: Combines two rotations. Notice that composition is non-commutative;
the order of operations changes the final orientation. |
* |
vector |
rotation |
vector |
Vector Rotation: Rotates the vector on the left by the rotation on the right.
(Note: Left-hand vector rotation is mandatory; `rotation * vector` is illegal). |
/ |
rotation |
rotation |
rotation |
Inverse Composition: Multiplies LHS by the conjugate (inverse) of RHS, effectively
rotating in the opposite direction. |
/ |
vector |
rotation |
vector |
Inverse Vector Rotation: Rotates the vector in the opposite direction (by the
conjugate of the rotation). |
+, - |
rotation |
rotation |
rotation |
Component-wise addition or subtraction. Not physically meaningful for normal rotation updates, but
valid mathematically. |
- (Unary) |
rotation |
N/A |
rotation |
Negates all components of the rotation.-q performs exactly the same rotation as q. This is because you are rotating in the opposite direction around the opposite vector and the two opposites exactly cancel each other. |
==, ~= |
rotation |
any |
boolean |
Checks for strict component-wise mathematical equality. Comparison with other types yields not equal.
|
and, or |
rotation |
any |
any |
Evaluates logical connections. Rotations are always treated as truthy. |
not |
rotation |
N/A |
boolean |
Always returns false. |
uuid type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
==, ~= |
uuid |
uuid |
boolean |
Checks if the 128-bit identifiers are identical. Comparison is much faster than string matching.
|
==, ~= |
uuid |
string |
boolean |
Always false: Unlike standard LSL, strings and uuids are strictly separated in
SLua. You must convert via tostring() or touuid() to compare them. |
and, or |
uuid |
any |
any |
Evaluates logical connections. All valid UUID values are treated as truthy.
(Note: LSL's NULL_KEY is still a valid uuid object, meaning it is evaluated as "truthy" inside logical conditions in SLua).
|
not |
uuid |
N/A |
boolean |
Always returns false because UUID instances are always truthy. |
| All others |
uuid |
any |
N/A |
Not Supported: Arithmetic, concatenation (..), and length
(#) are completely unsupported on raw UUIDs. Use tostring(my_uuid) to
manipulate it as string data first. |
table type
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
# (Unary) |
table |
N/A |
number |
Returns array table size. Triggers __len if defined; otherwise returns the boundary count of the array portion of the table.
|
==, ~= |
table |
any |
boolean |
Compares memory reference. If LHS and RHS are both tables sharing a metatable, triggers
__eq. |
+, -, *,
/, //, %,
^, .. |
table |
any |
any |
Raises an error unless the table defines its corresponding metamethod (e.g., __add,
__concat). |
- (Unary) |
table |
N/A |
any |
Raises an error unless defined in the table's metatable using __unm. |
<, >, <=,
>= |
table |
table |
boolean |
Raises an error unless tables share the same relational metamethods (__lt,
__le). |
and, or |
table |
any |
any |
Logical connections. Tables are always treated as truthy. |
not |
table |
N/A |
boolean |
Always returns false. |
other types
Other data types: nil, function, thread, userdata, buffer
| Operator |
Left Operand (LHS) |
Right Operand (RHS) |
Result Type |
Behavior / Description |
and |
nil |
any |
nil |
Returns nil (since nil is evaluated as falsy). |
and |
Others |
any |
any |
Returns RHS (functions, threads, and buffers are truthy). |
or |
nil |
any |
any |
Returns RHS. |
or |
Others |
any |
any |
Returns LHS (since LHS evaluates as truthy). |
not |
nil |
N/A |
boolean |
Evaluates to true. |
not |
Others |
N/A |
boolean |
Evaluates to false. |
==, ~= |
Any of these |
any |
boolean |
Checks reference equality. Userdata can implement __eq for value comparison. |
Shorthand (Compound Assignment) Operators
| Shorthand |
Equivalent Syntax |
Valid Left-Hand Types |
Behavior / Description |
a += b |
a = a + b |
number, vector, table
(with __add) |
Adds RHS to the current value of the variable. |
a -= b |
a = a - b |
number, vector, table
(with __sub) |
Subtracts RHS from the current value of the variable. |
a *= b |
a = a * b |
number, vector, table
(with __mul) |
Multiplies the variable's value by RHS. Handles vector scaling. |
a /= b |
a = a / b |
number, vector, table
(with __div) |
Divides the variable's value by RHS. |
a //= b |
a = a // b |
number, vector, table
(with __idiv) |
Performs floor division on the variable. |
a %= b |
a = a % b |
number, vector, table
(with __mod) |
Updates the variable to the remainder (modulo) of the division. |
a ^= b |
a = a ^ b |
number, table
(with __pow) |
Raises the variable to the power of RHS. |
a ..= b |
a = a .. b |
string, number, table
(with __concat) |
Concatenates RHS to the end of the current string. |