lua-users home
lua-l archive

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


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).
I suggested to introduce separate namespace for locals/upvalues (variable names without "$") for 100% checking for local/upvalue name typos at compile-time (not run-time).
Nevertheless, a "runtime-alarmer" would still be needed for detecting global variable name typos as run-time.


If you really hate global-by-default, you can disable it easily.


I do not hate globals :-)
I'm trying to solve the problem "the compiler silently ignores all variable name typos".
It's the expectation of many programmers that a compiler should check for misspelled identifiers and warn the user at compile time.
And the global-by-default feature is the only thing in Lua that prevents this to happen.
 
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".
"$" solves both these problems.