I want to know why setting the variables before hand makes a difference, and just a more clear explanation on how evaluation in Lua works.
Alright. I'll see what I can do.
Why setting the variables before hand makes a difference.
Short-circuit evaluation is when a later operation in a boolean statement is not evaluated because a previous operation in that same boolean statement is true. It is merely where the language does the minimal amount of evaluating to figure out the result of a boolean statement.
Note in a boolean statement. When point a variable to the result of an operation before the boolean statement happens, then the operation has already been evaluated.
There are no take backs in Lua; just because the result of that operation may evaluate to false in a later boolean statement, does not mean that the operation never happened.
Notice in your code...
function hello()
print('hello')
return true
end
function bye()
print('bye')
return false
end
local x = hello()
local y = bye()
if(x or y) then
print('check to short-circuit')
end
... you set x and y to the results of hello and bye. You know this because you wrote hello() and bye().
Notice that you called both of them. That means that those variables are now pointing to the evaluated results of the functions. You can check this by inserting a print statement between your local declarations and your if statement.
Remember that there are no take backs. This means that by the time you reached that if statement if (x or y) then, you already evaluated hello and bye. This means that naturally, those functions have already run once and printed out "hello" and "bye".
This is not a matter of short-circuiting, but more of a matter of you messing something up and attributing it to short-circuiting.
Basics
Evaluating boolean statements in Lua is as logical as it gets. Everything in Lua follows the standard properties of booleans, and there are no gotcha moments when working with Lua.
That being said, since you are asking this question, I'll give you a brief overview of how Lua works with boolean statements anyway.
The following will be considered false:
Everything else is true. That means that 0, "" or '' (empty strings), and {} (empty tables / data structures) will also evaluate as true, unlike in many other programming languages.
Here are some basic truth operations and their results...
print(true and true)
print(true and false)
print(false and true)
print(false and false)
print(true or true)
print(true or false)
print(false or true)
print(false or false)
print(not true)
print(not false)
Ternary
You may know that Lua does not have a true implementation of ternary operators. However, you can get by with a pseudo-ternary operation that Lua does support.
For most purposes, a and b or c will suffice. Lua will go from left to right and evaluate each statement. It will return the last statement in a combo of true statements connected by ands if the statement is true. Otherwise, it will keep going.
Here is an example...
print(true or false)
print(1 and 2 and 3 or false)
print({} and 2 or true)
print(1 and 2 and false or 3)
print(false and nil or false or 1 and 2 and false or 3)
Parenthesis
Note that parenthesis have a bit of priority. This means that Lua, when it encounters a parenthesis, first evaluates everything in the parenthesis before proceeding with evaluating the rest of the boolean statement. This means that you can have multiple short-circuiting effects and nested pseudo-ternary.
Here is an example...
print(1 and (false or 2) or 0)
Working with functions in boolean statements
Working with a function call is similar to working with parenthesis. First, you will evaluate what is in the function when you encounter it, similarly to how you would evaluate what is in a parenthesis. Then, you will proceed with the resulting value as you would regularly.
Here is an example...
function a()
return 2
end
print(false or a() and 3)
4 comments
You are calling the bye function when you do y = bye()
Yes, but when I just do 'bye()', I am also calling the bye() function.
It does not cancel out anything. You called the bye function when you set the variable. Your if statement is actually just
if true or falseIf my statement is just 'if true or false', then why does the print statement get called?