lua-users home
lua-l archive

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


I still think the main problem is non-intended assignment to globals,
which creates hard-to-find bugs. The simple policy of requiring a global
declaration to assign to globals would force programmers to consider
whether they really want a global variable.

-- Roberto


While unintentional assignment is the more insidious problem, in my experience unintentional reads due to typos is the more common bug by a substantial margin. We're using a lint-like tool to verify that all global reads are module level reads (essentially). It's working well, but it would be nice if Lua built-in support for something like this.

Another idea is to have each module (chunk) execute in its own environment, and global reads and writes go to this. That is, each chunk by default has its own _ENV (instead of defaulting to _G). If you couple this with a function "import" that loads a Lua file (unless it's been loaded before) and returns its _ENV, and you have the makings of a pretty good module system somewhat similar to Python's.

-- Tom