Lua: a general view (and personal) about the language

evandrolg
5 min readApr 11, 2015

The first time I played with Lua was around 2013, by the influence of a friend that was programming in the language for some time and told me good things about it. At the time, I have already had some years of experience with programming and I had had contact with different languages paradigms, in different expertise levels, such as Python, Ruby, JavaScript, Shell, C# and Java.

From that time until now, I programmed a lot in Lua and at some point I started to have it as my favorite language to develop outside working hours. In addition to small applications I made on weekends, I have been developing in my free time an open-source http server that is totally written in the Lua language, called Pegasus.lua.

I don’t have the most conventional skills within the community of Lua developers. I’m primarily a web programmer and the main use of Lua is as an embedded script language in C/C++ applications. This community focus is due to Lua being a high-level language that has a very fast and light interpreter (Lua's interpreter has less than 200kb!), unlike other dynamic languages such as Python and Ruby.

What has most called my attention in the Lua language is its simplicity. Having an extremely friendly syntax and a very lean core, Lua is a language that can be learned in some minutes by the ones who already have experience with programming (especially with interpreted languages). In practice, I consider Lua to be the easiest one I've learned, much because of the uncomplicated syntax of Lua but also because of my long experience working with JavaScript (yes, Lua is very similar to JavaScript, but in its good points!).

Below I list Lua's strong and weak points that I observed by the time I have been using the language:

Strong Points:

  • Lua has exactly eight types: nil, string, number, function, boolean, thread, userdata and table (where the biggest magics of the language live)
  • Like in Self, JavaScript and Io, in Lua you don’t implement Object Orientation through Classes, but using Prototypes.
  • Metatable is a great idea! Although in the beginning it seemed confusing to me, the Metatable concept is something extremely powerful in Lua. It’s an explicit way to make operator overloading and also extend the features of objects (tables).
  • The functions in Lua are first-class (also a similarity with JavaScript). It means that Lua allows the programmer to pass the function as parameter, declare it within a variable, store it in a data structure, all power JavaScript has to manipulate function is in Lua too. This flexibility to work with function in Lua is a feature that I like so much.
  • Closures support.
  • You export modules using just return. Ok, it’s just a detail, but since the first time that I saw it in Lua syntax I found it something extremely natural.
  • Corotines support.
  • The Lua interpreter is very fast in comparison to other dynamic languages such as PHP, Python, Ruby and even Node.js.
  • Easy implementation of Metaprogramming techniques (ok, it’s a dynamic language…).
  • Although Lua has a reduced number of core features, it allows the programmer extends the language features in a very simple way.
    - Do you want to have properties in your Prototypes? Ok, you can insert this feature in the language:
    http://lua-users.org/wiki/ObjectProperties
    - “I’m a Python programmer and I love decorators”. No problem, add this feature in Lua:
    http://lua-users.org/wiki/DecoratorsAndDocstrings
  • LuaJIT is one of the fastest Just-in-time for dynamic languages currently.

Weak Points:

  • There is a lack of a debug tool in the core of the language. I know many programmers that don’t make use of debug tools, but I particularly miss it and I don't always think it’s comfortable to debug my codes through print.
  • Lua doesn’t have the classic error treatment features such as try, catch and finally. It’s true that you can manipulate the error results using the pcall function, which I think it's not natural. Every time I need to deal and manipulate errors, I have been used this solution:
    https://gist.github.com/cwarden/1207556
  • Lua doesn’t have support for a ternary condition. It’s not a really important feature in the language, in my point of view, but I confess that eventually I miss it when I need to make some short validation that will be stored in a variable, for example. As in other features, ternary condition is also something that you can extend, creating a function that makes it:
    http://stackoverflow.com/questions/5525817/inline-conditions-in-lua-a-b-yes-no
  • There aren’t enough libraries in the community to resolve problems that are common in the programming universe. Maybe it can be even a characteristic of the community (developing small solutions inside bigger projects and not releasing these solutions as open-source) or a signal of that the language isn’t very popular. I’ve already had difficulties, for example, finding a reliable library to make parse of XML (I’ve just found a great XML library, but it’s not popular yet).

The points that I highlight as weak about the language are also understandable for a language that has as a preoccupation of keep having a light interpreter. The Lua's strong point that overlaps the weak ones is the fact of the language offers resources to extend its core features in a very uncomplicated way.

Lua, as any other programming tool, isn’t the “silver bullet”. For example, I definitely don’t consider it the ideal tool to be used in core of web applications. It’s not because of any limitation of the language, but for this lack of mature projects to deal with common problems in web environment — access to database, cache tool, MVC, etc. For these problems communities such as Python and Ruby have very mature libraries and frameworks and worldwide tested.

However, being a high-level language and having one of the fastest interpreters among dynamic languages, Lua is a great option to a big list of applications where the performance of the language is a problem (in chats, games, applications that need to run in embedded devices with limited memory).

--

--