Lazy operators in Lua - what they mean, and examples
Archive - Originally posted on "The Horse's Mouth" - 2014-05-05 18:37:02 - Graham EllisIf you use the word or in an expression in Lua, the expression to the right of the word will only be evaluated and returned of the value to the left is nil. So a very common idiom is:
stuff = stuff or {}
which means "if stuff already has a value, leave it alone - otherwise make it an empty table"
If you use the word and on the other hand, the expression to the right of the word will only be evaluated nd returned if the value to the left is not nil. This is a much less common requirement:
stuff = stuff and {}
which means "if stuff has a value, replace it with an empty table. But if it's nil, leave it like that"
Example from the Lua course completed at the end of last week - [here] and another example from earlier on [here].