use_strict

Name

use_strict – enable strict checking of global variable usage

Synopsis

-- Place this at the top of init.lua
-- (after "module(...)", if you are using that)
use_strict()

Description

use_strict() enables a mode where all usage of Lua global variables is strictly checked. If a global variable is used before it is first assigned to, then an error message will result.

The intention of this is to catch mistakes where a global variable name is misspelled. For example, if I write to a global variable named floor_tile, but then later refer to it as floot_tile (a typo), then in standard Lua, this would not result in an error message; instead, a value nil would be used for floot_tile. This might cause an error, or it might just create a subtle bug that takes a long time to debug, depending on what the code is actually doing with that variable. By enabling use_strict(), the typo can be found much sooner.

Return Value

None.

Errors

use_strict() itself shouldn't throw errors. However, if global variables are used incorrectly (after use_strict() mode is enabled) then errors will be raised, as described above.

Notes

If you do actually want to use a global variable with a default value of nil, then you will need to explicitly assign to that variable first, before using it. This can be achieved easily enough, by writing something like my_variable = nil near the top of your code. Then, uses of my_variable will not be classed as errors, because my_variable has previously been assigned to (even though the assigned value was only nil). The classic module uses this technique with a variable called Dsetup: see dungeon_setup.lua.

Examples

The classic module uses use_strict() in its init.lua.

See Also

module