Our beloved programming language, Lua, has recently received an update – the first major version change in four years. But what does it mean for regular programmers? Let’s find out.
Version 5.3 brings some very exciting, useful and previously sorely lacking features to the Lua built-in libraries. It’s difficult to list them in order of importance, so I’ll stick to the order given in the official changelog.
First of all, Lua now supports integers and floats as separate internal types. Though numbers stay as
number when calling
type on them, the new function
math.type will let you see into the core. So,
float. The newly added integers are implemented as
long long by default, which means they’re 64-bit, and range in values from 263-1 (
math.maxinteger) to -263 (
math.mininteger). Integers and floats behave similarly to statically-typed languages; An integer can be made into a float by either adding 0.0 arithmetically or appending .0 to the definition, and dividing integers with the division operator
/ will produce floats, but have no fear – the new
// operator enables quick integer division, or floor division, and eliminates the need to call
math.floor on your resultants. The new operator conveniently has a metamethod,
idiv, to go with it.
Bitwise operations have also been added natively as operators. What used to be
bit32.shift etc. has now become a set of familiar bitwise operators:
& for bitwise AND,
| for bitwise OR,
~ for bitwise XOR or unary bitwise NOT,
<< for left shift and
>> for right shift. These all coerce their arguments to integers and return integer results. Each of these operators also comes with a metamethod to boot:
Metatables themselves have received a bit of an upgrade - though it isn't something that's immediately apparent. They have, however, become even more reliable since 5.2, with
ipairs and the table library now honouring a table's metamethods when operating on it.
Another very useful addition is native string serialisation using
string.unpack. These two functions allow the user to serialise any set of objects, with the serialisation format defined by the user in the function call, and later deserialise them according to the provided format. This is exceedingly useful for storing data or transferring it over the network, and I'm very glad that the Lua creators have implemented such a versatile approach. The
string.packsize is a helper function that returns the size of a string serialised according to a certain format.
To summarise, this update has brought some very useful features to the core language. It isn't ground-breaking or revolutionary, but there's hardly place for a revolutionary update when the language is at this stage in development. Still, I'll be here, tinkering with the new features and trying to hack my way to pretty and efficient code - as is tradition.
The official changelog and links to the reference and manual are available on the official page for the update.