lua-users home
lua-l archive

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


> Eliminating globals. A speculation (as opposed to a proposal).
> 
> [...]

Lexical scoping has two sides. The first one is that a variable can
be used only when lexically inside the scope of its declaration. The
second one is that a variable can be defined only lexically where is its
scope. If a function can create local variables inside another function
in a different lexical structure, it breaks lexical scoping.

Another speculation:

I guess many people in this list agree that the problem of Lua is that
globals do not need declarations, and that it is easier to create a
global variable than a local one. We do not need (or want) to eliminate
global variables, only to make them more "difficult" to use.

The new _ENV mechanism may provide a simple solution to those problems.
In Lua 5.2, we can *always* write _ENV.x instead of x, with exactly the
same meaning (and creating the same opcodes). So, the compiler could
impose some restrictions on the use of 'x' by itself in some scenarios;
the user can always write _ENV.x if needed.

One option would be to ban assignment to globals at all inside
functions. Another option would be to only allow assignments to
global variable previously seen in that chunk.

-- Roberto