[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: New scoping rule for "repeat" and "continue" patch
- From: Rici Lake <lua@...>
- Date: Sun, 25 Sep 2005 10:38:13 -0500
I was interested to see that 5.1alpha implements a new scoping rule for
"repeat" statements; the expression in the "until" clause is now
*inside* the scope of the "repeat" block.
Unfortunately, this is not compatible with the semantics of "continue".
Consider:
repeat
local l = io.read"*l" or "quit"
if l:match"^#" then continue end
local g, rest = l:match"(%S+)%s*(.*)"
local done = handle[g](g, words(rest))
until done
If the "continue" statement is executed, the "until" clause will be
evaluated but the local "done" is uninitialised. Obviously, this could
be fixed by defining done before the continue statement, but Lua will
not detect the error, which I think is a showstopper.
If you want to test the code, here's a minimal test environment. You'll
notice that the loop is terminated by a comment (i.e. a line starting
#)
local function words(l)
local rv = {}
for w in l:gmatch"%S+" do table.insert(rv, w) end
return rv
end
local function defaultTable(def)
return setmetatable({}, {__index = function() return def end})
end
local function err(cmd)
print(("I don't understand what you mean by %q"):format(cmd))
end
local handle = defaultTable(err)
function handle.quit()
return true
end
function handle.hello()
print "Hello, yourself"
end