lua-users home
lua-l archive

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


Here's one way to avoid the issue in code like this and get rid of the globals problem.
a=1 --auto local
do
  a=2
  --is this a new global?
end
print(a) --what is it?

add one new keyword, global, and keep the local keyword.
Everything is local by default, not overriding any already existing locals, but the global keyword makes a global, and the local keyword creates a new one.

a=1 --auto local
do
  a=2 --same one
end

(Though 5.3work3 with _ENV changes a lot.)

On Wed, Jul 7, 2010 at 5:22 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> On 8/07/2010, at 6:00 AM, Roberto Ierusalimschy wrote:
>
> > 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.
>
> Could this be achieved this with a variant of strict.lua?

Sure, but only to a point. strict.lua is a dynamic check, not a static
one.  It still needs a minimum of tests to find errors. It does not
provide an easy way to circumvent the restriction. And it is optional.

-- Roberto