lua-users home
lua-l archive

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

On Thu, Jul 28, 2022 at 10:50 AM Lorenzo Donati <> wrote:
As I recall it, this is the right explanation for the current situation.

FWIW, I'd still like "break" to be improved to manage multi-level
breaks, even as syntactic sugar for an hidden goto+label definition.
I don't despise GOTOs, but I think that using a goto to exit a
multilevel loop is very error prone, especially if the label is far
away from the goto, which is not so rare a case when algorithm with 3 or
4 nested loops are used (and this is really the case where you would
find a multilevel break really useful).

Semantically a multilevel break is a very clear program action, which
should be understandable at the break site ("I want to exit multiloop").
Using a goto at that same point must rely on "meta" information to
convey the same meaning to the code reader, such as a comment or a
descriptive label, but these may be misleading (possibly ambiguous:
"goto start", to exit a multiloop).

I would welcome a syntax like "break 4", to exit four loop levels.
WRT the parser lookahead capabilities, maybe an uglier syntax could
avoid excessive lookahead, like "break<4>". After all with Lua 5.4 and
const/to-be-closed vars the syntax has already been uglified a lot, IMHO.

-- Lorenzo
I implemented continue N & break N into the parser recently in this personal project of mine, but it's very much an amalgamation implementation because I still allow Lua to create predefined break labels. I liked the feature a lot from PHP despite never using it.