lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Jul 26, 2022 at 2:36 PM Lars Müller wrote:
"return" is the only "retstat" and thus the only statement that is syntactically only allowed at the end of the block:
Why is this not extended to "break", which is a normal statement and thus allowed to precede arbitrary other statements?

"return" statement may have or have no parameters.
The Lua parser has to decide whether the lexemes following "return" are its parameters or beginning of the next statement;
this implies a non-trivial look-ahead.
To keep Lua parser simple, "return" was declared as the last statement in a block by the language syntax.
I personally consider this as a dirty hack :-)

The Lua team initially had plans about improving the "break" statement in the future:
"break" may be given an optional parameter for exiting multiple nested loops,
similar to the labeled break statement in Java.
That's why "break" in Lua 5.1 also was declared as the last statement in a block:
improved "break" will have the same parsing problem as the "return" statement.

But after introducing "goto" in Lua 5.2 the plan of improving "break" was cancelled,
because now you can break multiple nested loops with "goto".
Since "break" is now parameter-less now and forever, it was removed from "retstat".

 

Currently, Lua 5.2 to Lua 5.4 happily allow abominations such as

while true do break break break end
while true do break a = 1 end

Do you want to complicate the language syntax to guarantee that every statement is reachable?
It's not worth it.