2.3 KiB
Feature name
Goto statement
Summary
goto statement for jumps and making scripting a bit easier.
Motivation
Problems
Breaking in nested loops
Let's say for example:
while true do
for i=1, 100 do
if (i == 10) then
break -- This will only break the current for loop
end
end
end
In Luau there isn't a easy/simple way to break nested loops(without a variable)
Current solution we have:
local RunLoop = true
while RunLoop do
for i=1, 100 do
if (i == 10) then
RunLoop = false
break
end
end
end
The problems that this solution has are:
Messy: In a bigger scale this will get messy and confusing.
Uneeded checks: Why to do those checks when we could just jump out directly?
Possible solution
A goto statement would make Luau more easy to script and cleaner, if used properly.
while true do
for i=1, 100 do
if (i == 10) then
goto LoopEnd
end
end
end
LoopEnd: -- Could follow Lua syntax(:LABEL:)
Design
<ConstantString>:
Sets a label
Notes:
A label can't be used in another scope
function A()
FunctionStart:
do
if Y == Y then
goto FunctionStart -- However, this would work
end
end
if X == X then
goto FunctionStart -- This would act the same as a while loop.
end
end
function B()
goto FunctionStart -- Undefined label compile error
end
goto <ConstantString>
Jumps to a set label
goto <Number>
Jumps to X line
Notes:
If the number is a constant number, the compiler should optmize it
Otherwise if the number is a variable, any registers needed should be saved before the jump
If the X line is in another scope a error should be thrown
Drawbacks
Cross thread jumping could be a problem(but it would be very interesting if implemented and used correctly).
Lambdas/Functions that are called could bypass the scope boundary without runtime checks.
Alternatives
A break for nested loops
while true do
for i=1, 100 do
if (i == 10) then
:break -- The ':' means "break depth"
end
end
end