RE: [PATCH] experimental php-like 'break N' to break across multipleloops

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RE: [PATCH] experimental php-like 'break N' to break across multipleloops

Jerome Vuarand-2
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 : [hidden email] [[hidden email]] 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: [hidden email]
[[hidden email]] 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





Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] experimental php-like 'break N' to break across multipleloops

Karel Tuma
On Wed, Nov 01, 2006 at 05:45:28PM +0100, Jerome Vuarand wrote:
> 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
>
tagged/named loops have been discussed already:

http://lua-users.org/lists/lua-l/2005-10/msg00037.html

rici's solution, except being a bit intrusive to the language,
works well (testing the patch against 5.1.1 right now)

nevertheless, all of this is spaghetti code and should be avoided whenever
possible. i am of the opinion that multiple scope escape could be
useful only in very simple cases when its 100% clear what does the
code do... for that, break 2-3 does ok too. named labels would
even encourage some people (at least, me) to use such a thing extensively
turning maintainability of code to zero. as mentioned in earlier post, such a
functionality is basically "goto" and "label:" with few constraints in it.

 
> 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.

your tagged loops pushes the whole thing even further by sugesting ability
to dynamically specify the point of break scope - performance and
implementation complexity issues aside, thats "dynamic" spaghetti code
and could turn into real evil. one could suggest even:

while foo do
	eek()
this:
	orthat()
	while bar do
		goto(foo() and "this" or "that")
	end
	blah()
that:
	something()
end

eew. creepy, aint it? a bit like BASIC on a C64.
if you know how, one could write highly effective and (!)
obfuscated code.

yet its semantically similiar to your suggestion (without the artificial
constraint on point of destination)

note that i'm not "lobbying" for break N, it's just a suggestion for those
who want quick&dirty&unintrusive solution for _simple_ cases. from
language design perspective i stand behind the lesser scope magic,
the better. stock lua fullfils that statement to most extent
(except really ugly repeat local x = foo() until x).

//kt