Why is Lua such popular?

September 16th, 2007 by Ralf S. Engelschall

Background

A steadily increasing community exists around the programming language Lua, especially in the games related industry. In case you don’t know it: for instance, games like Far Cry or S.T.A.L.K.E.R. use Lua as an embedded scripting language. And there are Unix applications like RPM 5 and Monotone which also embed Lua as their programming language for scripting extensions. In RPM 5 one can code .spec macros in Lua via %{lua:...} and in Monotone one can hook into the run-time processing of the application by declaring Lua callback functions in ~/.monotone/monotonerc. Having such an embedded scripting language in an application is great for hackers like me as it allows to adjust and extend applications easily and flexibly.

Musing

But, why is the IMHO still somewhat “esoteric” and syntactically even partly strange language Lua used, and not for instance the more popular and both syntactically more appealing and semantically more powerful language JavaScript? What I’m talking about? Well, for instance: have you ever tried to find an employee who is able to code in Lua — versus someone for JavaScript?

Additionally, AFAIK Lua doesn’t even provide any really new language concepts others like Perl and JavaScript do not have. Sure, Lua is meant to be simple, but sometimes its simplicity becomes nasty. A little bit more functionality sometimes would be not too bad.

Common Idioms

And finally, C, C++, Perl, Java, and JavaScript at least syntactically share two common idioms: the braces for blocks and the semicolon as statement terminators — something we all got used to since decades! Lua OTOH uses then, do and end for blocks and the semicolon is just a statement separator in case one wants to put multiple statements onto the same source line.

Sorry, perhaps its just me, but when I have to code in Lua I constantly feel like I’m still 15 and sitting in front of my C64 and its BASIC interpreter. Sure, as a computer scientist with a strong focus on formal language parsing I know best that this issue is just about harmless “syntactic sugar”. But, sorry, IMHO it nevertheless hurts in 2007 when the whole world (just let us ignore the Python guys here ;-) already got strongly used to the block braces and statement termination rule.

“Simple API” for JavaScript to the rescue?

But, what I find most strange about Lua is the fact that IMHO its popularity doesn’t come from the fact that it is the greatest language since sliced bread, but because it has a small code footprint and (at least it seems at the first spot) to be easier embeddable into a C or C++ application. It is correct that here Lua has some advantages over complex APIs like Perl/XS, JNI, etc. But if THIS is the whole problem, why is nobody just coding a “simplification wrapper” for SpiderMonkey’s API (and bundle it as a out-of-the-box part of OSSP js ;-) and this way allow one to embed JavaScript into an application as easily as Lua.

If low footprint, then really low footprint, please…

Or to stretch the point even further: if low footprint is really the ultimate reason for Lua (which is 13K LoC) and a reason against JavaScript (80K LoC) or even Perl (105K LoC), then I still do not understand why people not even use for instance Arena (14K LoC) or even NASAL (5K LoC). Arena and NASAL both at least are a lot more C/ C++ /Perl/ Java/ JavaScript style in their look and feel and so at least attract the “old-style” coders a lot more.

Lua: the MySQL of scripting?

I really don’t want to be unfair, as Lua definetely is a rather well engineered solution. But I cannot resist associating Lua to MySQL all the time: a solution I personally always resist to choose because if I want really small footprint then I use SQLite (or Arena/NASAL in the Lua case) and if I really need sophistication and power I use PostgreSQL (or Perl/JavaScript in the Lua case). So, Lua certainly has its good place in the world, I just still do not understand this place well enough. And especially I do not understand why more and more projects intentionally use the “esoteric” Lua instead of the obvious “mainstream” alternatives.

Well, I’m still hoping I totally overlooked the absolute killer feature of Lua and that someone then will tell me that I’ve no clue and better should shut up. But even after coding a larger bunch of LoC in Lua I’m still not feeling “smart”. At least in all of the other mentioned languages I had this feeling rather quickly (after about a few thousand LoC). I think I will never really understand the attraction of Lua…

19 Responses to “Why is Lua such popular?”

  1. Markus Fischer says:

    I totally sympathize with you. LUA is the strangest thing I’ve ever seen. I came in touch with it with some tools to be used under Windows which can generated an ISO image on the CLI with very simple commands. When I looked under the hood and detected they were using LUA, I couldn’t be believe my eyes.

    Is it really all about the footprint? It this such a small area of development that it never reaches the surface like other languages do? I yet have to find a big forum where people talk about LUA.

  2. Thomas Lotterer says:

    I want to add a very practical and non-scientific note regarding the “braces for blocks” issue. Tools like the vim editor make it easy to identify the scope of block during daily program edits. When trying LUA, where such help is not available through generic nested bracket matching, I really identified the value of this (missing) aid.

  3. Clemens Hintze says:

    I am sure I have not the correct and final answer, but IMO Lua has several advantages, both for the programmer *and* for the user of an application that use Lua as an extension language. Here comes my unordered list, why I like Lua:

    1. A very simple unambigious syntax. You do not really need the ‘;’, you know? Lua is able to parse e.g.:

    a = 12 b = 14 c = a+b print(c)

    which may be feel strange for us programmer, but many non-programming user like this

    2. Its lack of several different data struture concepts! Lua has only one: table. That table type can be used to support a lot of concepts for that other languages need an own data type. For instance: arrays, lists, hashes, records what have you … even modules. But using C you can implement further data structures if you deem necessary.

    3. Although Lua has not a ‘typical’ OO system built-in, it has something I like better: metatables. Using those implementing an own OO system is a no-brainer. What do you want? A class based system with single or multiple inheritance? Or what about a prototype based one without inheritance but delegation?

    4. Beside this, Lua handle functions as values and introduce coroutines with a very clean and easy semantic.

    5. Its speed: Lua is among the speediest scripting languages around.

    6. Its small memory footprint; you already point that one out.

    7. Its extensibility: Lua is unique among those languages that may be extend via own C/C++ routines. I had to extend Tcl, Ruby, Perl, Python and a lot of other languages, but the way Lua does it is really different. I like it!

    As you can see, I really like Lua. Although I cannot use it every time I want (I still mainly have to use Ruby, Python and Perl) but it definitly had won me as a fan :-)

  4. Ralf S. Engelschall says:

    Ok, you’re right: point (2) and (3) I find impressive, too. They are certainly a a big plus for Lua. Although I’ve to confirm that JavaScript’s prototype based approach I like very much, too.

  5. Ric says:

    You’re being unfair mate! Co-routines, functions as first class values and all the flexibility in the world to do as you please with regards to OO (i.e. do you want single inheritance, you can have it; but you can also have multiple inheritance) – these are pretty compelling arguments for me. What about speed and ease of extensibility in C? Have you tried extending ruby, python or perl lately?

    I actually love python and ruby as well but lua is invaluable and deserves its space. I don’t think that you “get it” with Lua…

  6. Ralf S. Engelschall says:

    Ric: Ok, co-routines are certainly a standout feature of Lua, right. Functions as first class values is important, but this is nothing spectacular today — the “mainstream” scripting languages like JavaScript provide this, too. The way OO is implemented in Lua certainly is also interesting, but others provide more or less the same, too. Speed comparisons I’ve still not done myself, but you could be right that Lua is very speedy. The easy of extensibility in C seems to be the big plus for Lua. That’s why I don’t understand that the other languages do not provide at least a “simplified API” for this purpose. For Ruby, Python and Perl: yes, I’ve implemented bindings for those languages recently (for OSSP uuid). They are certainly more complex compared to Lua.

  7. Ralf S. Engelschall says:

    I’ve done some extensive amount of Lua hacking for RPM 5 over Christmas and New Year now. Ok, Lua is a nice toy which can be used for great applicaton hacks. Nevertheless its lack for an C/C++/Java/Perl-style “? :” operator and its very limited standard library bothered me really multiple times. But I was at least able to hack many exiting features into RPM 5 wih the help of Lua. Without Lua this would have been not possible, of course.

  8. Jeff Johnson says:

    Using “?:” is just a fetish. ;-) I also think that the “very limited standard library” is a feature for choosing an embedded language. Look at tcl, which started life as an embedded interpreter, but became too large and too complicated with the addition of “standard libraries”.

  9. Ralf S. Engelschall says:

    Sure, the limited standard library certainly is intentional. But a _little_ bit more in the direction of at least regex based string parsing would have helped a lot. But I have to admit that regex based parsing is not everybody’s favorite and I can image that the gaming folks (who use Lua a lot) might be especially not in the camp of regex lovers. But to make people like me, who are fans of regex (and actually formal mini languages in general), happy one needs to provide a little bit more parsing power. Nevertheless, as I said, Lua is a nice toy. Let’s just keep it this way ;-)

  10. David Lacerte says:

    Regarding your comment on a lack of “? … :” operator, there’s a common Lua idiom that uses the functional features of Lua to do the exact same thing:

    local var = cond and value1 or value2

    Just as the ?: C/C++/whatever operator, if cond is true, var gets value1 as a value, and value2 otherwise.

  11. Ralf S. Engelschall says:

    David Lacerte: thanks for the hint. I already tried “local var = if cond then value1 else value 2 end” but this was rejected as an “if” construct is not an expression in Lua. But your approach based on the boolean operators is something I’ve totally overlooked. Very interesting, although perhaps a little bit confusing to some readers as one has to understand the “short-circuiting” behavior of boolean expressions in Lua and that the “and” operator binds stronger than “or” operator. Nevertheless, I’ll use it…

  12. neri says:

    I started looking into lua lately. And I pretty much came up with the same questions. Then I stumbled upon a post on the lua ML that pointed out that Lua was started in the very early 90′s. And all of the sudden it makes sense. Even hardcore Lua fans say it has an oddball syntax, but as an embeddable scripting language everything counts, runtime and compile time. And the compiler, including the parser is so long around that it might be really hard to implement something faster or just as fast for a more modern syntax. For me, NekoVM comes close but thier language has limitations of it’s own(only while loops, I didn;t find any documantation about alle the built in $commands() so far etc.) Lua is just a tool that has been around forever and has not found a proper successor.

  13. Richard Hundt says:

    I think that the idea that Lua is ideally suited as an embeddable language — although true and possibly a driving factor in its development — is a little overstated and tends to discourage developers from taking it seriously, or at least trying it out. I don’t think Lua is any more quirky than Python with its white space, Smalltalk with its blocks, or Perl with its “line noise”.

    Moreover, I think that many programmers are happy to use the object model provided by their language (assuming an OO language, of course), and find that they don’t need more in their day-to-day work; but if you’re the kind of hacker who tends to push a language to its semantic limits, wishing you had more, then you’ll love Lua for its expressiveness and amazing versatility. And there’s beauty in simplicity, and Lua has plenty of that.

    It is not a strict language, however, so you need to be more disciplined, but I’d rather have it that way because I believe that expressiveness is a more important goal in a language than trying to protect the programmer from themselves.

    Finally, I think that Lua has a huge potential to serve as a common language runtime for dynamic languages, which is the goal of the Parrot VM. Lua strikes me as an ideal intermediate representation, and with the LPeg (Parsing Expression Grammars) library in the tool chain and a very clean C API, adding languages will be fairly comfortably do-able.

    So Lua may not be for everybody or for every project, but it’s definitely got a special spot in my tool bag.

  14. Demur Rumed says:

    Ruby has similar do/end syntax. Javascript doesn’t require semicolons (My ICS teacher also thought it required them for some reason). And when you say esoteric I think of languages from the esolang wiki. And Lua definitely isn’t from there

  15. Ksl says:

    Clemens Hintze: I would also add (8): Lua has tail calls. Thirty years have passed since the invention (or discovery?) of Scheme and there are still languages that claim to be “modern” (Python, for example) even though they have *no* tail call support. (Think of CPS style, simple state machines etc.)

  16. Felix Burton says:

    I would like to add (9): Lua functions can return multiple values. Of course this can be done in other languages by returning objects or lists, but I really like the simplicity of “return 1,2,3″ and “a,b,c = f()”.

  17. James says:

    I don’t know why anyone would believe the notion that Lua is not accepted amongst developers. The Game Industry considers it standard for scripting needs, and were talking about a $100 billion a year industry here.

    The reason it is loved by programmers is also why it is hated. It fits it’s niche perfectly and doesn’t attempt to be anything other than what it actually is.

  18. Dudu says:

    Ralf S. Engelschall says:
    “Sure, the limited standard library certainly is intentional. But a _little_ bit more in the direction of at least regex based string parsing would have helped a lot. But I have to admit that regex based parsing is not everybody’s favorite and I can image that the gaming folks (who use Lua a lot) might be especially not in the camp of regex lovers. But to make people like me, who are fans of regex (and actually formal mini languages in general), happy one needs to provide a little bit more parsing power. Nevertheless, as I said, Lua is a nice toy. Let’s just keep it this way ;-)”

    This seems to be a little(almost 3 years?) late .. Anyway as someone pointed out already if you are a regex lover you can’t miss LPeg (http://www.inf.puc-rio.br/~roberto/lpeg) =)
    Good luck.

  19. In the Code says:

    Lua is an amazing language but it’s not for people who start out criticizing a language for its syntactical nuances. If things like a lack of curly braces for defining blocks or a lack of a ternary operator (especially when there is a single-line idiom to do the same thing) are on your primary list of things to criticize while glossing over all the powerful facilities of the language, then it’s not the language for you.

    Lua’s main appeal among people who really appreciate it is its simplicity and directness. It is more like a building block for higher-level concepts. Without going into specific features, compare the brevity of the Programming in Lua book to books on any other language. It’s a language built on very few concepts that can do a lot. It doesn’t have need classes, for instance, since it has tables which can be used to effectively build them (the class keyword introduced in recent versions is just a shorthand). It doesn’t have all kinds of built-in aggregate types, it has tables which can be used to build them all.

    Lua is the type of language that can be best appreciated by those who appreciate things like the C++ standard library containers and the corresponding generic algorithms that operate on them. The powerful combination there of a small number of containers and generic algorithms that operate on them (and any other containers implemented using the same static interface) allow for a potentially infinite number of combinations with very little code. Beautiful architectures are generally considered those that can do a whole lot with very few rules and concepts. That’s an appeal that Lua has even at the language level.

Leave a Reply