Codea 2.3 Discussion

I’m working on the next version of Codea while 2.2 is awaiting approval.

I want to get your thoughts on upgrading to Lua 5.3, the latest version of Lua. I currently have an experimental beta with a functional Lua 5.3, but there are many incompatibilities with old code. I’d like to know if you think the benefits are worth it.

Lua 5.3 introduces the following nice features (among others):

  • Proper integer type, supporting integer division, bitwise operations
  • (integer division operator is //)
  • table.move, table.pack and table.unpack
  • string.pack / unpack to pack strings into custom binary representations
  • math.pow becomes x ^ y, many math functions updated
  • New utf8 library

Changes carried over from Lua 5.2 include

  • goto statement
  • No more fenv (i.e., setfenv / getfenv)

You can read more at (incompatibilities)

The main incompatibilities I have been running into when porting the example projects over are the following:

##Variable Function Arguments

function somefunc(...)
    if arg.n > 0 then
        -- do something    
        print( arg[1] )

Lua 5.1 introduced a new method for supporting variable function arguments called select, but also supported the old way of using a local table named arg. A lot of examples use the old (<= Lua 5.0) way. Lua 5.2 and above deprecate the arg table in favour of the new way, which is:

function somefunc(...)
    if select('#', ...) > 0 then
        -- do something    
        print( select(1, ...) )

##Implicit Integer Casts

This was one I found in @LoopSpace’s Anagram’s project:

function genNumber(number,variation)
    ret = variation*0.01*number
    ret = number + math.random(-ret,ret)
    return ret

This fails because the two-argument version of math.random now expects two integers, and a floating point number such as 17.5 is not implicitly convertible to an integer. So the fix is to do something like the following:

function genNumber(number,variation)
    ret = math.floor(variation*0.01*number) -- ensure float values are truncated
    ret = number + math.random(-ret,ret)
    return ret

There is a math.tointeger function, but it’s pretty strict. E.g., math.tointeger(17.5) will return nil, because 17.5 is not convertible to integer without losing precision. While math.tointeger(17.0) will return the integer 17.

I think that we should do this.

I’m all in for 5.3, despite what it will do to my project it looks worth it. One/two days fixing code is definitely worth the upgrade.

What would you expect from someone who hates changes and non-backward compatibility?
For me, the benefits dont overcome the disavantages, so i would stay as it is.
[edit] my opinion sounds like the lost battle of a lonely obscurantist, while i am only doomed to share Cassandra’s curse… I hate that. But BEWARE young advanturous fools, you’ll regret that move! Lol! %-( X_X [-(

I always like to try new things, but I don’t write anything worthwhile anyways. I would say go for 5.3, but I won’t be disappointed if we don’t.

EDIT: How’s that for not making a decision.

@Simeon are there many more incompatibilities that haven’t been mentioned or have they not been found?

I would say go for it. I remember times where I found a solution to a problem only to read it was lua 5.2 and above

I’ll do a beta of it once 2.2 is approved. (Apple TestFlight means I have to wait for them to finish reviewing before I can have another version out for testing.)

Lua will keep evolving, and if we don’t keep up, the eventual update will be much more difficult to develop and more disruptive for us users.

Pretty much the only downside to me is losing setfenv and getfenv… I was going to do some experiments with those :frowning:

@SkyTheCoder there are ways to simulate the setfenv behaviour

A discussion on why it was removed:

(Edit: Also you can still set the environment for a function with the _ENV table, it’s just more permanent.)

I’m not against if there is a real preformance gain (computations, iterators, function calls ?) and this does not break a lot of user’s code.
Also, it’s a good door to introduce new bugs in your codebase, but we all know that you like challenges :stuck_out_tongue:

After looking thru the above links about the changes, I think Codea should be updated. If we stay where we are because it might break some code, then we’re stuck where we are and it will just get worse to change later.

@Ignatz and @dave1707 are correct in this respect - I think Codea should be progressive in terms of its development path to keep pace with Lua’s evolution.

I guess some lack of backward compatibility is a small price to pay for upgrading to 5.3. I wonder if Codea could do some sort of ‘smart’ syntactic analysis that looks for obvious incompatibilities (e.g math.pow → x ^y) and makes suggestions? Although, I’d stop short of an ‘auto upgrade’ as I suspect this would be difficult to implement.

One curious addition is the dreaded ‘goto’ command - banished and frowned upon by programming cognoscenti for many years. I was trying to work out why a modern language would need it these days with such a rich set of other conditional constructs. Maybe I’m getting old :slight_smile:

@andymac3d I’ll probably keep math.pow around as a compatibility function.

The main issue will be the varargs usage, which is already deprecated in the current version of Codea (Lua 5.1 deprecated it), but I think people continue to use it because it shows up in Google as a way to do varargs in Lua.

goto is fine as long as you don’t abuse it. In some cases it can make code clearer (i.e., you don’t have to keep track of a boolean variable to break out of two nested loops). goto in Lua 5.3 is pretty limited too, you can’t just jump around in a completely adhoc manner — there are some rules.

Some examples of goto in Lua:

(Nested break is probably the most common use case)

@Simeon If GOTO works like I used to use it, you can’t do a GOTO out of the function it’s in. That limits its use.

It doesn’t need to be used much, though. Nested breaks would be the main use because there is no better alternative.

Probably best is to stick it on testflight, and then we can all run through our library of apps and see what breaks. The big issue will be breaking changes to the wider community, but if from testing not too much breaks then this isn’t a big issue. Regardless it’s probably worth pushing forwards to the latest.

And then we just need to build up some wiki on expected things to break and how to fix them.

My opinion in this is that ‘we’ should indeed update it to lua 5.3, because as the others already mentioned, we can’t fall to far behind.

I’m sure that there’s going to be people on here who will manage to overcome most, if not all, ‘problems’ with the backwards incompatibility.

I’m in the update camp - I always try and keep my code as simple as possible and I can’t see anything that would explictly break any of my projects. I think the use of proper integers and bitwise operators is reason enough. As an aside does the use of integers make much of a speed difference?

Anything that helps to improve Codea is never a bad idea and I for one would welcome the changes,