Add 'goto' and 'continue'

I got bored, so with some inspiration from @spotco’s message about moonscript, I “lent” the LuaSyntaxToolset from @stravant’s Minify&Beautify plugin and threw some code together around it which can turn this:

for i=1,4 do
	if i == 1 then continue end
	if i == 3 then break end
	print(i)
end

into this:

for i=1,4 do local _u_0 = false repeat
	if i == 1 then break end
	if i == 3 then _u_0=true break end
	print(i)
until true if _u_0 then break end end

which, when ran (I just loadstring() it), outputs 2.

not sure I’m even gonna try to do labels, let alone goto, cuz would be a bit… tricky, to say the least

EDIT: Made it that both “continue” and “continue()” work, so editors don’t complain

1 Like

I don’t think it’s meaningful to add this kind of fluff to lua’s syntax when we could be getting useful built-in functions from 5.3 like os.date (can’t wait for this) or table.move instead. Chances are that when you actually do need either goto or continue you really just need more functions.

Please excuse the nature of this argument, but Dijkstra considered goto harmful within the context of it being used by wise elderly cybermancers with actual degrees and fancy titles and whatnot. As such, I don’t think goto would be beneficial to inexperienced or beginning programmers nor the quality of their programs. Structuring stuff is essential and very hard (almost as hard as naming stuff) so having a tool to break any structure at any time could be very detrimental to their ability (to learn) to structure their programs properly.

Of course none of that’s relevant to us, but it probably is to the platform as a whole in some capacity.

4 Likes

Goto is really really useful for breaking out of multi level loops and control structures.
If goto is implemented, then it removes the need for continue.

But people usually consider it evil because if you don’t know what you’re doing, it tends to lead to spaghetti code. Of course, “goto doesn’t spaghetti code, people spaghetti code.”

Won’t get added though, because it breaks away ROBLOX Lua further from the original 5.1.4 or whatever. :frowning:

4 Likes

One thing I’ve liked about Java is that it allows you to label loops, which lets you break out of them. Probably would be easier to implement goto, but the Java label feature is what first came to mind when you said that:

hello:
for (int i = 0; i < 10; i++) {
	for (int j = 0; i < 10; j++) {
		if (j == 3) {
			break hello;
		}
	}
}

(Although a lot of people consider it bad practice to use labels in Java too. Can’t win.)

2 Likes

I’ve always felt like break break should be used in this situation. Shame that’s not a thing.

3 Likes

I’m pretty sure in the 5+ years of coding on, I wished for the continue thing many times (no, don’t ask me for use cases. Not about to go through 3+ year old code to find things.)

goto sounds like fun, but seems like there’s no chance for it now. :frowning:

Sigh.

1 Like

Same. Mostly for things like:

for idk in whatever do
	if idk:Something() then continue end
	-- do several lines of things
end

as I prefer just having one “when to ignore” line than wrapping everything in an if-statement.

4 Likes

Why do I keep forgetting foreach is a thing!

I need to start using it!

I don’t see the point of this, truly. Effectively you’ve taken apart an if statement.

for a = 1, 5 do
if a == 2 then
print(a)
end
end

As for the second bit, functions work just fine.

function Done()
--blah
end

function Not_Done()
--k
end

function Init()
for a = 1, 5 do
if a == 2 then
Done()
return
end
end
end

Init()

At best? I see it used for organization or shortening bits of code which could be useful in some cases, but otherwise I just can’t see myself using it.

2 Likes

It was just an example of how it is used. Not an example of a good use.

Right, right. I’ve never really used it before so I don’t have as much of a view for how it could be beneficial.