Posted 12 October 2016 - 02:34 AM
Recently, I've been suffering a lot with the lack of continue statements in Lua. I searched a bit, and found that Lua 5.2 added goto, which can be used to approximate continue (and greatly simplify the code):
Before those not-even-gonna-say guys say something stupid and the topic is locked, yes, this is useful. For example, I want to skip the current iteration of the loop, without stopping it or filling my program with ugly if statements. (That's the very definition of the continue keyword.) See:
A solution is to, as I said, fill the program with ugly ifs:
for i, v in ipairs(something) do
if some_action then
do_stuff
else -- Failed; next item
goto continue
end
other_stuff
::continue::
end
So, went to test it and… nope. Just like the safe part of the debug lib. And others.Before those not-even-gonna-say guys say something stupid and the topic is locked, yes, this is useful. For example, I want to skip the current iteration of the loop, without stopping it or filling my program with ugly if statements. (That's the very definition of the continue keyword.) See:
for i, v in ipairs(lines_of_code) do
if test_for_command then
for stuff in list_of_stuff do
if can_do_with current_thing then
do
-- what do I do now? I wanna stop!!!! I already did everything!
els.. i mean end
-- You can't else a for loop!
do_it_another_way
end
end
other_actions
end
A solution is to, as I said, fill the program with ugly ifs:
for i, v in ipairs(lines_of_code) do
local failed = true
if test_for_command then
for stuff in list_of_stuff do
if can_do_with current_thing then
do
failed = false
break
end
end
if failed then
do_it_another_way
end
-- Etc.
end
other_actions
end
This really gets in the way when the program gets big.