Add loop keyword 'continue' to Rbx.Lua

Like this thread, but only focusing on the continue keyword.


continue is not present in Vanilla Lua, however it proves to be extremely useful in loops and its applications, having been added into so many numerous languages like;

  • C family languages (C/C#/C++)
  • JavaScript
  • Java
  • Python
  • Visual Basic
  • etc

The keyword is used like this;

for i = 1, 10 do
   if i % 2 == 1 then
      continue
   end
   print(i)
end

Like with break. This prints every even number from 1-10.
This is just an example on its syntax, not an actual usage case.

As a Roblox developer, I find consistent needs to use continue. continue is useful for many applications. An example is data get retries; where if it fails, it can continue the loop to retry the data and keep going, instead of trying to rely on break, being confused in the process, or writing unnecessary lines. continue allows developers to have more flexibility when writing code concerning loops and why does Lua not have it?

What doesn’t make sense is that Lua has break, but not continue. The languages I listed above that has continue, has break as well. Why does Rbx.Lua, being based off of vanilla Lua, has break but decide not to add continue? I understand it may be hard for Roblox to perhaps change Rbx.Lua’s internal functions, and even implement the case itself, nevertheless has a wide range of use cases.

“There’s nothing “clear enough” about writing code without continue. It’s a novice mistake to nest code inside a conditional where a continue should have been used, and the need to write ugly code like that shouldn’t receive any sympathy. There’s absolutely no excuse.” – Glenn Maynard Sep 12 '15 at 23:56

(a reply found from the topmost answer in the StackOverflow linked)

It will be very helpful for Roblox developers if this feature can be considered.

9 Likes
for i = 1, 10 do
   if i % 2 == 0 then
      print(i)
   else
      continue
   end
end

I see no need for continue

for i = 1, 10 do
   if i % 2 == 0 then
      print(i)
   end
end
9 Likes

That was a bad example (I changed it)

Still, only shows how it works

I very much dislike the idea of adding new syntax to Roblox Lua. It would make unit testing nigh impossible if it used any of the new syntax, but I understand that’s a niche case.

The problem is continue isn’t all that useful. I’ve always preferred just splitting the code inside the loop into functions and calling them from there.

3 Likes
for i = 1, 10 do
   if i % 2 == 1 then
      continue
   end
   print(i)
end

New example, but its still redundant

for i = 1, 10, 2 do
   print(i)
end

I can see where you can argue that you can do something before the continue as to not call the print call, but an else statement easily fixes that.

4 Likes

I … really don’t see a use for this. Nice to have? Sure, I’ll give it that. Honestly tho? We already have what we need with if then statements. It’s just a matter of how you perform logic checking is all.

5 Likes

This doesn’t solve the possible need of an user who has to skip an iteration of the loop while already being nested into if statements or in the middle of some logic because of a condition that needs to be met. continue would be a nice addition to solve whenever this is the case.

4 Likes

I’d love to have it because

for stuff do
    if not condition then continue end
    doOtherStuff(stuff)
end

is better in my book (cleaner, less indentation, potentially less lines – I already do it with return) than

for stuff do
    if condition then
        doStuff(stuff)
    end
end

but this is debatable and subjective. I’m thinking not worth the effort, not worth the effects, but hey. If it can be done well, do it.

2 Likes

I mean …

for stuff do
  if condition then
    break
  end 
  doOtherStuff(stuff)
end

Besides being a different way to write code, I still don’t see it as a need other than a nice have/eye candy. Please do show me if I’m not fully understanding it’s usefulness tho, I just don’t see it at this moment.

2 Likes

I like having continue in languages, but very weary to add new stuff into an existing language like this.

I also feel like I only need to use continue a couple times a year. Usually it’s easy to restructure the logic to avoid using it.

3 Likes

For similar behavior, you can make a function and use return:

for i = 1, 10 do
	(function()
		if i%2 == 1 then
			return
		end
		print(i)
	end)()
end

the downside here is that if the loop itself is inside a function that it may want to return in then it will return in the wrong function. there is a solution to that, though:

for i = 1, 10 do
	local function continue()
		if i%2 == 1 then
			return continue
		end
		print(i)
		return continue
	end
	local value = continue()
	if value ~= continue then
		return value
	end
end
1 Like

It’s also possible to use a wrapping repeat ... until loop and breaking out of that.

for i = 1, 10 do
    repeat
        if i % 2 == 1 then
            break -- "continue"
        end
        print(i)
    until true
end

Of course, neither method really beats having the keyword built in.

Using repeat is also an order of magnitude more efficient computation-wise than using functions.

local t = tick()
for i = 1,1000000 do
	(function()
		if i%2==0 then
			return
		end
	end)()
end
print("func",tick()-t) -- Around 0.36 seconds for 1,000,000 iterations

local t = tick()
for i = 1,1000000 do
	repeat
		if i%2==0 then
			break
		end
	until true
end
print("repeat",tick()-t) -- Around 0.03 seconds for 1,000,000 iterations

While it might be faster, I think I still would avoid this if possible. It’s just confusing code.

Edit: But do whatever is needed for what you’re doing honestly. There’s definitely times where you need to break away from standards in order to complete tasks.

2 Likes

That’s all this entire feature is: eye candy. Well, to me anyway. Other people with more advanced uses may have something for you in that area, such as ANSI_C and dogwarrior24.

continue isn’t a reserved word in Lua, so if we were to added it, it would break compatibility with external code even further than we already have.

It would add confusion, as this is a feature that is not documented in the Lua manual, and we don’t (yet?) offer a fully featured alternative to the manual.

And it’s easy to workaround, albeit at the expense of rightward drift. If your rightward drift is severe, you should factor out code into functions. Having your indentation get more than like 6 deep is a code smell regardless of whether you have continue or not.

For syntax sugar like this, you have to think of it as starting at -100 points (or maybe even -1000 points, as this would be extending the core Lua language, which we largely have not done yet). It takes a pretty convincing argument for us to make a change like this.

11 Likes

I agree with you here, I wouldn’t personally use it in most cases. But for people that will, it’s useful to know which is faster. Many are surprised at how slow function declarations and calls are.