[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Globals (more ruminations)
- From: Roberto Ierusalimschy <roberto@...>
- Date: Mon, 12 Jul 2010 10:41:02 -0300
> 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.
Unintentional reads are much more common, but also much easier to find.
> 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.
This demands only minimal discipline. Just write "_ENV = {}" in the
beginning and "return _ENV" in the end of each module. "import" is
"require".
-- Roberto