Lua in 15 minutes

Lua in 15 minutes

Sergey Abbakumov

I found a mini-course on the Lua language, which gives the basic syntax: http://tylerneylon.com/a/learn-lua/.

In general, this language is used in the game industry (and not only), so I decided to look at it more closely.

Lua seems strange enough to me as a person with C++ and Python background. Here are some of its features:

• Comments start with

--


and multiline onces with

--[[
--]]


• All variables are global, but it is possible to create a local variable through the local keyword (they say, JavaScript took this feature from Lua).

• All numbers are floating point. The authors state that they are not worried, because 64-bit real numbers can exactly store integers of 52 bit width.

• Blocks of code are denoted as in Pascal via do/end, then/end.

• If the variable is not declared, but it was read, the result is not an error, but nil.

• Only nil and false are counted as false. Everything else is true. Even 0 and ``.

• In the for i = 1, 100 cycles, the range includes both boundaries.

• There are full-fledged closures.

• Functions are first-order objects.

• The only composite data structure is a hash table. All the rest, including arrays, lists, trees, is constructed through tables. As the authors themselves note, this idea was inspired by Lisp, where everything is created on the basis of lists.

• "Arrays" are indexed starting from 1.

• OOP is prototype-driven (this, they say, is similar to JavaScript). It's implemented using tables, meta-tables and meta-methods. Generally it's pretty amusing concept.


The first edition of the Programming in Lua book is freely available from the authors on the language website: https://www.lua.org/pil/contents.html.


In general, the language is rather strange, but interesting. However, I find Python much more intuitive and straightforward. The only thing you can praise Lua for is that the implementation only takes a few dozen files in C, as well as for the incredible compactness and speed of its virtual machine and its high compatibility with C, which makes this language very useful in game development.


Telegram channel: https://t.me/sea_plus_plus

Report Page