lua-users home
lua-l archive

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


On 9/07/2010, at 10:16 AM, Henk Boom wrote:

> On 7 July 2010 22:44, RJ Russell <russellsprouts2@gmail.com> wrote:
>> 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.
> 
> 'Local by default' has problems, though. If you prevent implicit
> locals from shadowing outer ones, as you suggested, it becomes easy to
> introduce certain types of bugs:
> 
> function f()
>  ... lots of code here ...
>  ... and more ...
> 
>  local function g()
>    a = 1
>    ... do some stuff with a ...
>  end
> end
> 
> Now you have the problem that if, during maintenance, you declare a
> variable that happens to be called 'a' in f, or outside f even, scopes
> change and g breaks subtly.

We have that already in the opposite sense.  If your assignment to "a" in g() was a deliberate assignment to a global, then declaring "a" local at the top of f() would change the meaning of the assignment in g().

(I'm not saying this is an argument either way, just pointing it out)

Geoff