lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 5 Jul 2018, at 06:52, Egor Skriptunoff <> wrote:

On Wed, Jul 4, 2018 at 8:41 AM, Daurnimator wrote:

We already have an opt-in of this behaviour:
local _G = _G; local _ENV = nil
and then use e.g. `_G.print

On Wed, Jul 4, 2018 at 1:25 PM, Dirk Laurie wrote:

local next, io, print = next, io, print
--  etc: first cache all the globals you need
local _ENV = setmetatable({},{__index=error, __newindex=error})

These are two more "yet another run-time alarmers".
They are not equivalent to my suggestion.
Any "runtime-alarmer" is unable to find all local variable name typos (for example, inside currently inactive "if" branch).
All existing solutions are actually run-time checkers, they are unable to solve the problem.
And simultaneously I want to solve another problem "globals are accidentally shaded out by locals with the same name".

Already solved.  See , also the discussion over the previous week.

This doesn't work in global scope, only in function scope, but it allows you to control the accessibility of globals *and* upvalues (which has largely been missing from this discussion) and generates compile time errors if an unknown variable is accessed.  The syntax can only work at a scope change because of the way the compiler works, so in some cases you may need an extra closure to get the behaviour you want.

I've been running this in all of my Lua code for 2 1/2 years now.