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
becomesx ^ 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
http://www.lua.org/manual/5.3/readme.html
http://www.lua.org/manual/5.2/readme.html
http://www.lua.org/manual/5.3/manual.html#8 (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] )
end
end
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, ...) )
end
end
##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
end
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
end
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
.