lua-users home
lua-l archive

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

On Thu, May 12, 2022 at 8:46 PM Egor Skriptunoff <> wrote:
Probably the "lexical scope" problem is about "repeat-until" loop in Lua:
  if ... then continue end
  local finished = ...
until finished
Here "continue" is impossible to implement (both as a keyword and a goto-label) as it jumps inside the scope of the local variable "finished".
The language design choice was to
- either disallow referring inner local variables in the "until" condition (you have to define "finished" variable before "repeat")
- or disallow the "continue" keyword.

IMO, the ability to use inner locals in the loop exit condition is a much more useful feature than "continue" keyword.
Of course, Lua can disallow using the "continue" keyword in some loops and allow it in other loops, but it looks too weird to be included in the language, instead another weird but versatile operator ("goto") was introduced.
Now perfectionists want to either remove "break" or introduce "continue", because both these operators are just a hidden "goto" :-)

Introducing "goto" introduces the same issues as adding continue, and a few more. Lua 5.4 refuses to compile the code below because you jump over the definition of a local variable. You could apply the same test to a "continue" statement. If you jump backwards over a local variable declaration that variable goes out of scope in Lua. Most other languages just ban this type of goto unless you exit the block completely.

    if math.random() ~= 0.1 then
        goto continue
    local finished = true
until finished

Adding "continue" would not add any problem that adding "goto" did not already introduce.