[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Questioning variable scoping in Lua
- From: Olivier Galibert <galibert@...>
- Date: Tue, 20 Jan 2009 14:04:50 +0100
On Tue, Jan 20, 2009 at 12:27:16PM +0100, Ben Aurel wrote:
[why not local by default]
> I think I miss something here. Could somebody explain me the reason
> behind the design decision?
There already have been discussions on the list on the subject, but to
sum it up the main issue is that local-as-default does not work very
well in an optional-variable-declaration environment. When you write:
[...]
local a
if(...) then
a = ...
end
(use a)
[...]
and
[...]
if(...) then
local a
a = ...
end
(use a)
[...]
you do not get the same result. Declaring a local variable also
declares its actual scope, and the narrowest possible score around the
first assignment is often not the one you want. So you'll have to
declare half of the locals anyway.
And then, when it comes to debugging, it is relatively easy to detect
accesses to global variables and warn about if we want to, than try to
guess whether a given assignment or access is a new local variable
creation or an access to an already declared local variable which had
too narrow a scope.
So, when you have optional variable declaration, only global as
default makes sense. If safety is your primary issue, you need a
statically typed, mandatory declaration language anyway. Not a
dynamic and loosely typed, optional declaration language like lua.
OG.