lua-users home
lua-l archive

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


We would just have to tag loops and use that tag in break statements. Example:

local prefix = "loop"
local i=1
local n = 2
while[prefix..1] i<=10 do
   for[prefix..2] j=1,10 do

      if foo then
         break "loop1"
      elseif bar then
         break prefix..n
      else
         break -- Would trigger a runtime error, no loop has nil tag
      end

   end
end

Simples "while", "for" and "break" would be syntactic sugars for "while[nil]", "for[nil]", "break nil" respectively. "nil" as a loop label match only "nil" as a break parameter, no wildcard. I used brackets because parenthesis after while/for/repeat would collide with current syntax. For the break it feels more natural to not have any parenthesis, but we could enforce () or [] with existing sugars for string and array constructors.

I think it's a nice addition. It probably needs internal Lua support because of the tagging of loops, and error detection if break parameter match no current tag.

-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Paul Hudson
Envoyé : 1 novembre 2006 09:43
À : 'Lua list'
Objet : RE: [PATCH] experimental php-like 'break N' to break across multipleloops

I'm not sure this is needed, but if other people disagree :) then I think the Perl-esque idea of breaking out to a labelled statement is the way to go.

The problem with using numeric levels is that refactoring the code and changing the number of nested scopes means looking for and changing any "break N" statements, which I think is a likely source of tricky-to-debug errors.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Karel Tuma

hi listers,

i've found myself doing lot of miserable things today, due to inability of lua to simply break out of multiple nested breakable scopes in one statement.

finding nothing useful in powerpatches archive i wrote this rather naive and unintrusive patch - so there is generally no reason why it shouldn't work, but i've tested it extensively only with various for/while nested loops.

use:
while foo do
	while bar do
		if baz then eek() break 2 end
	end
end