Continue Proposal |
|
Nearly every other language on the planet has this equivalent. Sure, you can restructure your code to get around this limitation, but it's not nice. Arguments against "continue" within loops can be equally applied to "return". Sure, with such a restriction you can restructure your code into towers of if/then/else, but it's very annoying. Being able to "return" from the middle of a code block is exactly like being able to "continue" in a loop -- it's a convenience and it doesn't hurt readability.
So this is my plea for "continue". If it's simple to implement, why not?
--DanHollis?
You can do this using lua errors :) the code may looks like:
while cond do local b, err = pcall(function() ...some code... error("continue") -- the 'continue' equivalent : ...some code... end) -- if there is another error : if not b and err ~= "continue" then error(err) end end
Unfortunately, you loose the stack traceback in case of errors ... you can use xpcall and debug.traceback() to keep it :)
I would also prefer to have a continue statement :)
--Mildred
I do not understand the lack of "continue" in Lua. This is used as often as "break" in C (and other languages). Its use has benefits: it does not oblige us to find tricks to simulate it when needed (in our algorithms). I vote for it in a next version of Lua. -- JulioFernandez
I doubt that "continue" is really used as much as "break". K&R actually says it's used less often. --JohnCowan?
I have just revised the patch (in LuaPowerPatches) to Lua 5.1.3, along with a small test suite to prove that it works. -- WolfgangOertl