Tricks with Lua
From WoWWiki
| | This article or section may need to be wikified to meet WoWWiki's quality standards.
|
See the Lua documenations complete details.
Logical Short-cutting
Lua "short-cuts" the evaluation of logical expressions. If you have the Lua expression
x and y
if x is false or nil, it doesn't evaluate y at all. If the y expression contains a function call, that function is never called.
Likewise, for
x or y
if x is logically true (i.e. anything other than nil or false) then y is never evaluated.
Short-cutting to Flag Programming Errors for Unexpected nil Values
Let's say you do some evaluation, and the result is supposed to be non-nil. And if it is nil, this is a programming error that should never happen, as opposed to the kind of error that could come from bad user input. You can flag this kind of error easily.
local valueThatShouldNotBeNil = (exp) or error("something went wrong");
Mind you, your user is going to see that error fat in the middle of their screen, so this really better be a show-stopper. But the alternative may be a lot of coding to handle a case that isn't supposed to occur anyway, or failing within the next few lines trying to index or index with nil.
= Disclaimer
(Still under construction.)
