lua-users home
lua-l archive

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


On Sat, 23 Feb 2008 10:20:37 -0300, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:

>Maybe a (partial?) solution to the implicit global problem would be to
>follow the style of strict.lua, but at compile time. There would be
>only one rule:
>
>- an assignment to an "unseen" global inside a function raises a
>compile-time error.
>
>"Unseen" means that that global has not been used before in that chunk.
>
>To use a global inside a function, you may need to "declare" it. If the
>global belongs to the chunk, a simple "name = nil" in the global level
>will do.  Otherwise you may use something like "name = name". It is
>strange, but it is (or should be) uncommon for a function to mess with
>globals declared elsewhere.

I'm in favor of this. It would have saved me some trouble last week. I
have a pretty big Lua code base and it is getting hard to keep track
of the globals. I think it should be a compile-time option, though, so
as not to require working code to be modified to fit the new rule.

>(Question: in "a = a + 1", should the second 'a' be a valid "declaration"
>for the first one?)

I think that should still require a "declaration", to help avoid
accidental use of a global "a" when you weren't meaning to.

Steve