[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Automatic Variables Local rather than Global (Proposal)
- From: "Ricardo Ramos Massaro" <ricardo.massaro@...>
- Date: Mon, 4 Sep 2006 13:21:34 -0300
On 9/4/06, John Hind <john.hind@zen.co.uk> wrote:
As a long-time user of Lua in an environment where multiple scripters
share a common global table (Girder 4), there is just one thing in Lua
I'd really like to see changed. This is the automatic creation of
variables in the global environment table.
[snipped]
I think this is a bad idea. Consider this example:
function f()
function g()
a = 1
end
a = 2
end
With your proposed change, would the two "a"s refer to the same
variable, or would each assignment introduce a new one? I don't think
there's an easy and elegant way to make a compiler do the first (and
it would make the language specification much more convoluted), and
the second looks really ugly (this is not exactly, but related to one
of my biggest problems with PHP).
There's also the "problem" of functions already mentioned. I guess it
could be resolved by saying that "function f()..." is really "global
f; function f()..." -- although I must confess that, coming from a
Scheme background, I don't like this arbitrary separation of functions
and variables.
I really prefer the forced declaration ("option explicit") you
mentioned to resolve this "issue", i.e., don't allow reading from
variables without a previous assignment. I believe this can be done
currently by munging the global environment metatable.
I would take it a step further: don't allow ANY use of undeclared
variables, and make it a compile-time check (like Perl's "use
strict"). But this would introduce the need of a keyword ("global") to
declare global variables.
Regards,
Ricardo
PS: I've been reading this list for some time, but this is my first
message. Please excuse my bad english.