Allow to break out of multiple loop levels in a single 'break' instruction

As a Roblox developer, it is currently too hard to handle the output of multiple nested loops.
Currently, to be able to break out of all loops, I need to have additional boolean controls like:

local ok = false
repeat -- loop1 
	repeat -- loop2
		repeat -- loop3
			if condition then 
				ok = true
				break
			 end -- it will break to loop2
		until false
		if ok then 
			break
		end
	until false
	if ok then 
		break
	end
until false

Luau could create something similar to PHP: break - Manual which would make it much easier to get out of multiple nested loops.

repeat -- loop1 
	repeat -- loop2
		repeat -- loop3
			if condition then break 3 end -- it will break ALL 3 loops
		until false
	until false
until false

If Roblox is able to address this issue, it would improve my development experience because this will make it much easier to control the output of aligned loops, making programming more synthetic and intuitive.

4 Likes

A workaround is to create a function and return.

I wouldn’t support this feature as it feels messy to use.

5 Likes

This would be more intuitive if the loops could be named, ie

repeat loop1
  repeat loop2
    break loop1
  until false
until false

However, this creates unnecessary complexity into the compiler. Also, afaik, the bytecode format has no way to control loop order in this way.

Plus, how would this translate to while loops and for loops which would be harder to attach a name to.

1 Like

I submitted an RFC for this a while back and the response seems to be “will be reconsidered at a much later date.”

4 Likes