Jump to content
Toggle menu
  • 17.3K articles
  • 8.2K files
  • 5 users
  • 280.7K edits
WoW Classic Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

HOWTO: Do Lua Tricks

From WoW Classic Wiki

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.)