why do some people do
local value = false and -1
why do some people do
local value = false and -1
These identifiers are known was “idioms” and act as if statements. What your code does is if value
is false, set it to -1.
If I wanted to check if a value is 5 and set it to 10, if it is. I can do that by if statements and idioms.
I’ll do it with idoms:
local val = 10
val = val == 10 and 5 or val --> if val is 10, set it to 5 or to val(5)
The reason for the or
is because if val
is not 10, it will be set to what it is currently. You can’t expect val
to be 10 all the times and if it isn’t, val
will be set to false because the idiom evaluated to false.