Continue Proposal |
|
continue
statement that is used to terminate current iteration of a while
, repeat
, or for
loop, skipping to the evaluation of the condition for the next loop iteration. Just like return
and break
statements, continue
can only be written as the last statement of a block.
For the official motivation for not including continue
in Lua, see [1]
Pros:
if
, thus less identation levels.
Cons:
continue
is the right or better choice when also considering for inclusion features like goto
, labeled break
, and others.
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?
return
is not quite like continue
because it serves at least two additional purposes besides terminating a function: (a) identify the values returned by a function; and (b) make it easier for the compiler to identify tail calls. --RenatoMaia?
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 have just revised the patch (in LuaPowerPatches) to Lua 5.1.3, along with a small test suite to prove that it works. -- WolfgangOertl
I support this proposal. The main advantage for me is "continue" clearly and correctly expresses the sense of the algorithm, the intent of the programmer. Less complicated or shorter code is just a nice side-effect. An issue is both "continue" and "break" are ambiguous (quit current iteration or overall repetition?). For this reason, I find "next" much better for the proposed semantics. "next" is more accurate by itself and "next"+"break" resolves "break"'s ambiguity. ("next"+"exit" may be even better but it could conflict with the sense of "os.exit".) -- Denis
continue
in standard Lua
if
Usually, you can emulate continue
by changing the following code pattern
while invariant do <START BLOCK> if condition then <MIDDLE BLOCK> continue -- nothing is allowed here and there is -- no sense in adding an 'else' here end <FINISH BLOCK> end
into
while invariant do <START BLOCK> if condition then <MIDDLE BLOCK> else <FINISH BLOCK> end -- here you have the chance to add a -- finalization code that is always executed end
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
break
It's easy (though somewhat verbose) to simulate "continue" in pure Lua. --GregLudwig?
Here's a simple idiom for simulating a continue
statement in Lua.
The basic idea is to use a local continue
variable and the break
statement inside a repeat
-once block.
We want implement continue
as in the following example:
for line in io.lines() do -- start BODY if string.find(line,"c") then continue end if string.find(line,"b") then break end print("-->",line) -- end BODY end
To accomplish this, replace BODY with:
local continue repeat -- start BODY 'BODY' replacing 'continue' with 'continue=true; break' -- end BODY continue=true until 1 if not continue then break end
Thus the example becomes:
for line in io.lines() do local continue repeat -- start BODY if string.find(line,"c") then continue=true; break end if string.find(line,"b") then break end print("-->",line) -- end BODY continue=true until 1 if not continue then break end end
Reduced indentation at the expense of the continue
functionality (i.e. always continue) can be written like:
for line in io.lines() do repeat if string.find(line,"c") then break end print("-->",line) until 1 end
In Lua 5.2.0beta-rc1, you can write the above example using a GotoStatement like this:
for line in io.lines() do if string.find(line,"c") then goto continue end if string.find(line,"b") then break end print("-->",line) @continue: end
See