[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Questioning variable scoping in Lua
- From: "Alex Davies" <alex.mania@...>
- Date: Tue, 20 Jan 2009 21:59:21 +0900
Has been discussed many times before (google will show many counter
examples). By leaving it as globals, you can trap accesses (via
metamethods) to undeclared variables which can help find those cases. Also
as an example of just one problem with local by default:
function ternary(cond, iftrue, iffalse)
if cond then
res = iftrue
else
res = iffalse
end
return res
end
How often would you forget to write local res at the top of that function?
If you don't, (with local by default) it'd just return nil. (Of course this
is quite a contrived example, but it's not hard to see how often you'd still
need to write "local" to ensure it has the scope of the correct block)
Besides, having to write local is still at least as easy as declaring c
values at the top of their scope.
- Alex