lua-users home
lua-l archive

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


Hi Andrew,

Thank you very much for the references. 

I see that this issue has already been discussed before and some people even patched Lua to get continue and even break N and continue N (I didn't want to go that far, I just wanted my little old "C" one-level continue).

Of course one can use workarounds for anything, but if you don't give me continue I'll stick to conditional blocks, because IMO the other workarounds (special for functions, coroutines, tail calls, etc.) are too complex and syntactically contrived for the purpose.

In my personal experience I have found many cases where continue is very handy. If you have several points in a long loop body where you would like to test a condition and eventually end the current iteration and you are forced to use a conditional block, you introduce one additional level of indentation at each decision point. This can be very annoying:

    while something do
       bla
       bla
       if contition1 then
          ble
          ble
          if condition2 then
             bli  
             bli
          end
       end
    end

Using continue stramlines the loop and IMO makes it much more readable:

    while something do
       bla
       bla
       if not contition1 then continue end
       ble
       ble
       if not condition2 then continue end
       bli  
       bli
    end

And this loop body has only two decision points! In practice it frequently happens to have more.

IMO the conditional-block version suffers from "Pascalness". Enforcing a nested control structure does not necessarily enhance the comprehensibility of the code. On the contrary, many times the code becomes clearer when you can streamline the main flow of control (bla bla ble ble bli bli) and eventually depart from it when some condition is not met.

The Pascal paradigm was that every block of code should have a single entry point and a single exit point. The first idea was very good and caught: no reasonable programming language today allows you to jump into the middle of a block. The second idea is wrong.
See Khernigan's article, "Why Pascal is not my favorite programming language".

This does not mean that jumping anywhere from the middle of a block is OK. But practice has shown that it is good and useful to be able to abort the execution of the current block by jumping to the end of it, and even to abort the execution of some enclosing block by jumping to the end of that block. That is the whole idea of continue, break and return.

When I think of a loop body I imagine three nested blocks: the body block representing the current iteration, surrounded by the loop block, whose code controls the iteration, surrounded by the currently executing function. Break lets you jump to the end of the loop. Return lets you jump (conceptually) to the end of the function. Continue lets you jump to the end of the current iteration. All too natural.

Plus, continue does not introduce any new syntax, only semantics, since it is syntactically legal exactly in the same places where break is.

Well, I guess all this has been said before and probably solved by the patches I've seen. I just wonder why it is not officially accepted into the language.

Thanks again and best regards,
Hugo


Andrew Wilson escribió:
Hi Hugo,

Here are a few references to previous discussions about a continue
statement, seems people work around it or use tail recusion.

http://lua-users.org/lists/lua-l/2005-09/msg00527.html
http://lua-users.org/lists/lua-l/2003-06/msg00454.html

Some customization too, have been made to add continue to lua...
http://lua-users.org/lists/lua-l/2003-06/msg00306.html

Regards
Andrew

On 1/24/07, Hugo Etchegoyen <hetchegoyen@hasar.com> wrote:

 I have been working with Lua for only a few weeks, and I like the language
a lot. It offers a wonderful combination of size, speed, simplicity, ease of
learning and openness so that anybody can extend it and tweak it to taste.

 But being a seasoned C/C++ programmer, there is only one thing I miss a
lot: a continue statement! Please, please, I know that people asking for new
features to be added to a language usually annoy its designers, and
generally for good reason. But I honestly think this would be a great
addition which would cost practically nothing and would benefit the language
and make it more coherent.

 Pascal was a coherent language: it religiously avoided anything that could
break the structured flow of control, so it did not have returns, continues
or breaks. It did not even have elseif! That is why most Pascal programs are
unreadable when they have more than two or three levels of nested ifs and
they tend to overflow the right margin unless programmers use ugly and
confusing indentation tricks.

 Clearly Lua is more like C, since it has return and break. But the absence
of continue complicates the body of loops. Consider this loop, for example:

     for i = 1 to somevalue do
        if some_condition then
           ...
           ...
           ...
        end
     end

 Wouldn't this be nicer?

     for i = 1 to somevalue do
        if not some_condition(i) then continue end
        ...
        ...
        ...
     end

 I know it's a matter of taste, but also of coherence. IMO Pascal was
coherent (but ugly) and Lua is beautiful but still lacks this last touch.

 Regards,
 Hugo Etchegoyen