lua-users home
lua-l archive

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

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:

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
	while bar do
		goto(foo() and "this" or "that")

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