why lua 5.1 instead of 5.2

was just wondering why CODEA uses 5.1 rather than 5.2?

wanted to use goto to emulate continue statement but its not in 5.1

Maybe re architect your code? Make a function, possibly recursive?

Omg, they implemented goto into lua5.2? This alone is a good reason not to use it :wink:

I agree with @Spielkind. The goto is a bad idea (read some Dijkstra). The ability to go anywhere in your code makes for unreadible and unstable code. Try doing what @aciolino suggests and redesigne your code or find another way to emulate a continue (I’m assuming your talking about the Java continue block). One way to work around a continue that I have used in the past has been to add an if-statement to catch and then increment the counter inside the loop before you execute code in the loop. I do know that this will not work with all languages, but it does work in a lot of languages. For example:

for i = 0, n do
    if condition then
        i = i + 1
    end
    -- Code here
end

Like I said, this may not wor, but it will do what a continue statement does in Java. Also, please correct me if I am wrong as that can be the case… :smiley:

a) goto can be evil if misused, but it also can make error handling more readable in some cases. We should leave it to kernel code an the like.

b) The correct way for a continue is rather this:


    for i = 0, n do
        if condition then
            -- continue, i.e. do nothing here
        else
            <loop code>
        end
    end

Yes, this can be written more compact.

c) blmacbeth, your code is as evil as a misused goto and turns against another safe programming advice: you shall not modify the loop counter inside a loop.

Let’s take the following example:


function doNotOperateOnIndex(i) do
    return i ~= 5
end

This function is a written in a way so we can use it in the code given above that uses “condition” to perform a continue. The function effectively allows only the value 5 to perform an operation.

Putting it in your suggestion:


    start = 0
    n = 10
    for i = start, n do
        if doNotOperateOnIndex(i) then
            i = i + 1
            print("Continuing on " .. i)
        end
        print("Operating on " .. i)
    end

Play with the values of start and n and you will see:

c1) Your code will operate sometimes on 5, sometimes on 6.

c2) Your code will also operate on other index values.

c3) Your code may generate a loop value of i that is n + 1.

d) Avoid evil constructs. Then you can skip calling goto evil.

If you are doing a continue, reverse your if logic and use functions for the logic.

You all have valid points.

However, “if then elseif then else end” can become unreadable very fast when all you want to do is the classic continue to the next step of the loop — a very limited, disciplined use of goto.

Oh by the way, under all the hidden stuff in lua, the lua virtual machine has a jmp instruction that is a goto that it cannot do without. That’s true of all computer languages and computers.

The only single answer is readable documentation and lots of it tied tightly to the code. That’s what my Coding On Napkins project is all about.

Ok, here is a good ‘pro and con goto’ chapter in ‘code complete’.
http://goo.gl/E4nz0
And yes, there are pros and cons.
This is a good book btw. and not specific to any programming language.

Good link on the topic.

@aciolino: yes, reverse logic would have been better, but I wanted to stay close to the given code to show the fallacies.

@CodingOnNapkins: The “if then else end” appears because I wanted to show the path of the continue. If you have “if then elseif then else end”, that sounds like an “elseif” too much for a classic continue.

Result: Continue, we continue to miss you. Did you go to where goto came from in 5.2?

My opinion: continue, even in java, is still bad, or at the best very lazy, coding practice.

Dad horse, we continue to beat you. Did you stay when everyone else wanted you to goto?

truth be told I’m surprised no one has mentioned the potential for a stack overflow when returning to prior used functions.

While I admit it can be rare, if done excessively (or even recursively if you will) you can still get that error.

But otherwise its true you can use conditional statements in tandem with function callbacks to return to certain lines of code even across tabs.

On a quick note, it is very VERY rare when you ABSOLUTELY need goto so why not take advantage of functions and classes.
Anyways, one of the main reasons TLL isn’t update the Lua is code compatibility. Wjphile the are things I’d like in v5.2 of Lua, I don’t want a third of the code written in Codea to go sour.

i really hope that 5.2 is much more backwards compatible than your statement makes it seem :slight_smile:

There are few, but significant differences. Also, I think a big factor is how much would have to change in Codea’s back end.