std.strict 1.4
Contents
Modules
- std.strict
std.strictDiagnose uses of undeclared variables.
All variables(including functions!) must be "declared" through a regular
assignment(even assigning nil will do) in a strict scope before being
used anywhere or assigned to inside a nested scope.
Use the callable returned by this module to interpose a strictness check
proxy table to the given environment. The callable runs setfenv
appropriately in Lua 5.1 interpreters to ensure the semantic equivalence.
| strict (env) | Require variable declarations before use in scope env. |
| strict | Module table. |
| env:__index (n) | Detect dereference of undeclared variable. |
| env:__len (t) | Proxy len calls. |
| env:__newindex (n, v) | Detect assignment to undeclared variable. |
| env:__pairs (t) | Proxy pairs calls. |
| __call (env[, level=1]) | Enforce strict variable declarations in env. |
| __index (name) | Lazy loading of strict submodules. |
Normally the module strict:__call metamethod is all you need, but you can use this method for more complex situations.
env proxy table with metamethods to enforce strict
declarations
local _ENV = setmetatable({}, {__index = _G}) if require 'std._debug'.strict then _ENV = require 'std.strict'.strict(_ENV) end -- ...and for Lua 5.1 compatibility, without triggering undeclared -- variable error: if rawget(_G, 'setfenv') ~= nil then setfenv(1, _ENV) end
len calls.
pairs calls.
stack level for setfenv, 1 means
set caller's environment
(default 1)
_ENV
local _ENV = require 'std.strict'(_G)
the submodule that was loaded to satisfy the missing
`name`, otherwise `nil` if nothing was found
local strict = require 'std.strict' local version = strict.version