lua-users home
lua-l archive

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


On 01/28/2011 08:07 AM, Roberto Ierusalimschy wrote:

In Lua, we favor generic and powerful constructs. Actually, a 'goto'
would be almost perfect.  But its interaction with variable scoping
is quite confusing in a language with first-class functions like Lua.
Labeled breaks seem the next best thing, as it allows any forward goto
that does not enter a variable scope.


I understand this. A 'break' jumps forward to the instruction following the indicated 'end'. One of the problems with 'continue' is whether or not to execute the conditional. The example you showed,

    while cond do
      do :inner:
        if something then break :inner: end
      end
    end

Will retest the loop, but how would you restart the loop without testing?

    while cond do
      while true do :outer:
        do :inner:
          if something then break :inner: end -- doesn't test
          break :outer: -- does test
        end
      end
    end

If 'continue' were to be defined as a backward jump to the instruction following 'do' then you could restart the loop with just the one inner 'do'. (Labels on 'while', 'for' and 'repeat' loops aren't even needed, except as sugar.)

    while cond do
        do :inner:
          if something then continue :inner: end -- doesn't test
          break :inner: -- does test
        end
      end
    end

Unless there is a a reason to restrict jumps to forward only. So long as the destination is at either the start or end of a lexical boundary, I think it would be okay.

I suppose the counter-argument is closures. (Made less bulky by 5.2's prototype caching.)

  while cond do
    local inner = function() do
      if something then return inner() end -- doesn't test
      return -- does test
    end
    inner()
  end

And I might have missed the explanation, but why two colons?

--
- tom
telliamed@whoopdedo.org