dh_Toolkit Tab
7.08 Lua Notes:

While learning to code in Lua I was somewhat dismayed by the amount of inconsistent, and sometimes contradictory documentation. Two things that had me scratching my head I will address here.

Lua types:
Variables:
Variables are data stored at a memory location. The data type is either a value type or a reference type. The data stored is the value, and the reference is the address. When a value is declared [local A] it is given a name. When a variable is assigned [local A = 2] it is given an address, and the data is stored at that address. The data will have a type. When a variable is read it goes to the memory address to read the data. If a variable is assigned to another variable [local B = A] the result depends on the type of the pre-existing variable A. If A is of a value type, B is assigned a new address and a copy of the data in A is put in the address referenced by B. If A is of a reference type, B is assigned the address of A and references the same data as A.
Value types:
number, string, nil, and boolean are value types.
Assigning a variable with a value type to another variable COPIES the value.
a = 25   -- a holds the value 25
b = a    -- b now has the value 25
a = 32   -- a now holds the value 32
         -- b still has the value 25
Reference types:
table, function, userdata, and thread are reference types.
Assigning a variable of reference type to another variable COPIES the reference.
a = {1,2,3,4}  -- 'a' holds the memory address where the value {1,2,3,4} is located. 
b = a          -- 'b' now holds the memory address where {1,2,3,4} is located. 
a = {5,6,7,8}  -- the memory address that 'a' references now holds the value {5,6,7,8}
               -- the memory address that 'b' references has the value {5,6,7,8}
Ternary operation:

I have come across code statements that addled me. Upon investigation I realized that they were attempts to utilize a ternary (composed of three parts) operation.

A ternary operator is an expression that returns one result if a condition is true, but a different result if the condition is false. It is a concise form of the logic achieved by using an if-then_else statement.

if condition then
    return value_if_true
else
    return value_if_false
end

Lua lacks a built-in ternary operator, but it can be mimicked by using the logical and...or operators. This works as a side effect of how Lua evaluates the and...or operators. All three parts of the ternary expression can be a value, a variable, or an expression.

Lua returns the last operand evaluated that results in a true condition. An and operation evaluates all its operands.If all are true it returns true. An or operation stops evaluation at the first operand that is true. In Lua all values except for nil and false are considered to be true.

The expression is evaluated from left to right.

result = condition and value_if_true or value_if_false

For brevity let's rewrite it as:

result = A and B or C
Since all operands of an and operation must be evaluated we can rewrite it as:
result = (A and B) or C

If both A and B are true the expression (A and B) is considered true and C is not evaluated. The last operand evaluated is returned.
If B is a value or variable that value or variable is returned.
If B is an expression the result of that expression is returned.
Say B is 4 + 2. 4 + 2 equates to 6 which is considered true. Therefore Lua will return 6.

If either A or B is false the expression (A or B) is considered false and C is evaluated. The return result is evaluated as above.
The "ternary" operation does differ from the aforementioned if-then-else statement. It is more comparable to:
if (condition == true) and (value_if_true == true) then return value_if_true else return value_if_false end

I think my original confusion was because of the length and complexity of the "ternary" statement I came across. Sometimes, for clarity's sake, it would be better to use an if-then-else statement.