What Lua 5.1 code breaks in Lua 5.2?
See the official incompatibility list.
8.1.1 Arg for variadic functions
The pseudo-variable arg
in functions with indefinite arguments (...
) is no longer defined. See here.
8.1.2 Must explicitly require debug library
If you need the debug functions, then say debug = require "debug"
.
8.1.3 unpack moved to table.unpack
unpack
takes a table and returns all its values, e.g. x,y = unpack {10,20}
. Easy to do a local unpack = table.unpack
at the top of your files.
8.1.4 setfenv/getfenv deprecated
In short, there are no longer any function environments in Lua. There are new ways to handle common usages.
8.1.5 module deprecated
This is likely to be the major breaking change, since module()
is now commonly used when writing Lua modules. module implicitly uses the deprecated setfenv
and in any case received criticism for causing global namespace pollution.
It is probably best to use a plain 'no magic' style for defining modules, where every exported function is explicitly qualified, and the table of functions explicitly returned.
local _M = {}
function _M.one()
return _M.two()
end
function _M.two()
return 42
end
return _M
Notice that this module does not need to be explicitly named, it may be put anywhere on the module path and required accordingly. But you cannot assume that a global module table has been created, so always use an alias.
local mymod = require 'libs.mymod'
You can of course use your module name instead of _M
, this is just to show that the name is arbitrary.
8.1.6 newproxy removed.
This was always an 'undocumented' function in Lua 5.1, and it was considered unnecessary, since it was chiefly used to write finalizers. Since the __gc
metamethod now works for Lua tables, this workaround is no longer needed.