lua-users home
lua-l archive

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



Am I missing something here?  Did you actually run the code?

The b=1,10 do ... end loop is having its own local b, and 'b=1' is retained after the loop.

Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> b=1
> for b=1,10 do print(b) end
1
2
3
4
5
6
7
8
9
10
> print(b)
1


David Given kirjoitti 21.2.2008 kello 15:16:

...
Oh, I see. You're right, I'd misunderstood you.

However, this still means we end up with the following situation:

a = 1
...56192 lines of code snipped...
function foo()
  for b = 1, 10 do ... end
end

foo() has it's own local b. Now I change the first line to:

b = 1

...and now foo() is modifying the outer-scope version. In other words,
making a keyword at the beginning of the file causes a semantic change
in the meaning of some code 56193 lines later --- not ideal if you want
robust code.

Give that your original suggestion was that global-by-default causes
pollution of the global namespace if you forget to declare a variable,
I'd suggest that this approach to local-by-default causes pollution of
the local namespace instead --- which I think is exactly the same problem!

- --