[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: proposal (RE: Making vars local by default)
- From: "John Belmonte" <jvb@...>
- Date: Thu, 31 May 2001 22:45:15 +0900
"Tom _" wrote:
> I think people don't generally appreciate how serious
> of a problem the current semantics are. Accidental
> use of global variables is a sleeper: it may be
> present in your code for a long time and not show up
> until you make completely unrelated changes to your
> code. The consequences of such bugs can be very subtle
> and very hard to track down.
The issue is real and some of us do appreciate it, although I think it's a
tolerable annoyance and not a show stopper. Having to switch to a new
assignment operator like ":=" would be much more annoying.
Have you looked at "globals.lua" in the ./lua/test directory? That can
pinpoint global writes very well. (By the way I noticed a bug in that
script. It prints instruction numbers instead of line numbers. To correct
it, replace the two occurrences of "(%d+)" with "%[(%d+)%]".)
Also I'm going to attach my util functions that I use to diff the list of
global variables. The advantage of this method is you can diff any two
points in your program execution, so if used correctly it won't bother you
with standard library globals and such.
By the way I like Russell's idea of the Lua authors reserving the keyword
"global" for future use (or user hacking). Just in case, you know...
-John
########
-- GetGlobalNames - returns hash table of current
-- global names
--
function GetGlobalNames()
local names = {}
for i,x in globals() do
names[i] = 1
end
return names
end
-- DiffGlobalNames - shows diff of current global names
-- vs previously recorded
--
function DiffGlobalNames(t)
local gtable = globals()
local deleted = {}
local added = {}
for i,x in t do
if not gtable[i] then
tinsert(deleted, i)
end
end
for i,x in gtable do
if not t[i] then
tinsert(added, i)
end
end
sort(deleted)
sort(added)
print("Changes to global names:")
print(" Deleted")
for i = 1, getn(deleted) do
print(" "..deleted[i])
end
print(" Added")
for i = 1, getn(added) do
print(" "..added[i])
end
end