lua-users home
lua-l archive

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

John wrote at 6/11/2009:

> Not sure if any existing language does this, but it strikes me that
> composite loop structures would be a more elegant way of implementing this
> kind of thing:

> local done = false
> for a in foo until done do
>     for b in bar until done do
>         if something then done = true end
>     end
> end

Two deficiencies I can see:

#1 This looks semantically the same for this case, but opens
possibility to write some hard to read code. If you write something
after that "if", and you don't use a "break" there, it will run the
code once, and then exit. How can you tell whether that was intented
or a typo (forgotten "break")?

#2 Due to ability to do #1, the compiled code would be slower, as it
has to check an additional condition in each iteration of each of the
two loops. AFAIK, as Lua byte code _does_ have a forward jump
internally, doing a real jump (either written as a goto, or as a named
break, would be more performant, I believe.

> labelled jumps are ugly and error-prone, particularly if they are limited by
> non-obvious restrictions.

Any restriction enforced by the compiler (and this one is easily
enforceable, I believe) cannot be error-prone, I think. The resulting
code is very readable, and you cannot make mistake when writing code,
as it won't compile then.