[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Automatic Variables Local rather than Global (Proposal)
- From: Niklas Frykholm <niklas@...>
- Date: Tue, 05 Sep 2006 09:50:18 +0200
John Hind 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.
I'm torn on this issue. I think forcing declaration of local variables
with "local" is good since it makes the scope of variables clear
(especially important when you work with closures), but it is too easy
to forget to write "local".
Having a "global" keyword would fix that, but having to declare all
global functions "global" would be a slight bother (not too bad,
though), and I wouldn't want to have different rules for functions and
other things.
Currently, I do the following to spot missing "local"s:
---
function lock_new_index(t, k, v)
error("module has been locked -- " .. k .. " must be declared local", 2)
end
function lock(t)
local mt = getmetatable(t) or {}
mt.__newindex = lock_new_index
setmetatable(t, mt)
end
function f(y)
x = y * y
print(x)
end
function main()
f(3)
end
lock(_G)
main()
// Niklas