top | item 6192972

What makes Lua tick?

154 points| da02 | 12 years ago |lua-users.org | reply

108 comments

order
[+] sramsay|12 years ago|reply
Lua is indeed very, very aweseome -- for all the reasons enumerated, and then some.

But your average Rubyist/Pythonista shouldn't be fooled. The ecosystem of Lua assumes you eat C for breakfast, and if you don't, you're going to be looking around for easy, plug-and-play libraries and wondering why they don't exist.

They don't, because in Lua world, you either call C from Lua or embed Lua in C. You don't throw something together in Lua using the happy friendly libraries that others have written for your convenience (often, in C).

I see comparisons being made with Lisp here and there. The most truthful comparison is sociological: as with the Lisp community, the Lua community (which is very friendly, approachable, and helpful) largely assumes you're smart enough to build it yourself. In C.

If that frightens you, you should probably go back to whatever you were doing before.

[+] compay|12 years ago|reply
> The ecosystem of Lua assumes you eat C for breakfast

That's a bit of an exaggeration. There isn't anywhere near the number of libraries that Python and Ruby have, nor anywhere near the general level of quality, but you can do an awful lot without ever touching C. Reams of Lua code have been written to script WoW by people who have no idea how to program in C. In fact, one of the primary design objectives of Lua is to have a language that's simple for non-programmers to learn.

> you're going to be looking around for easy, plug-and-play libraries and wondering why they don't exist.

That's very true. I spent the better part of a year working on open source web development libraries in Lua but eventually gave up because nobody was using them. There are fantastic libraries in Python and Ruby, at this point it's not really worth reinventing the wheel just to do the same stuff, but in Lua.

The interesting thing about the Lua community is that very few people use Lua as their primary day-in-day-out language, which leads to a distinct lack of fanboyism. I think that's in part why so few libraries exist. The typical Lua programmer attitude seems to be, "Why should I rewrite that in Lua when I can just use the one written in <insert language here>?"

[+] meddlepal|12 years ago|reply
But is that the attitude the community should be taking? Why can't it be both? In fact it would doubly-awesome if that were the case because having a language that has great support for C but can still be used with ease is a gap most languages fail to close because they choose the binary one or the other.

What can be done to address that?

[+] NelsonMinar|12 years ago|reply
My favorite thing about Lua is that the language is so simple, once you figure out one way to do something it's probably the only way. Like, say, summing up all the numbers in a list of strings. In Perl there are many ways, in Python there are many ways but one is right, and in Lua there's probably only one way so once you figure it out, you're done. Simplicity is a nice thing in an embedded tool language.
[+] Kaali|12 years ago|reply
Lua is a really simple language, and is quite malleable to different use cases not included in the language per se. Usually in my book, it is an advantage, for example how many Lisps can implement new features that seem like language features, but aren't.

But this simplicity can have a downside, as there might not be canonical ways of doing things that users are accustomed to. For example, object orientation. It could be a hard sell to someone coming from an object-oriented language, when you have to understand, and choose between multiple different styles of object-orientation and implement them consistently in your project, or use one of the many libraries made just for this. For example, see http://lua-users.org/wiki/ObjectOrientedProgramming

Lua really is simple, but it not always easy.

[+] eieio|12 years ago|reply
As a huge Python fan, comments like this(and many others) have made me want to check out Lua. Is there a canonical Lua introduction out there? I'd love to give it a try.
[+] speeder|12 years ago|reply
It is not like that... Most things can be done in many ways, some of those in excessively many ways...

It just happen that for most projects any of.them are "right" except the most esoteric ones...

You can fully pull some crazy "clever" stuff, but requires effort, usually the first idea in your head is already good enough.

[+] sheng|12 years ago|reply
would you mind to state some of the so many ways of summing up all the numbers in a list of strings for python? And give a counter example for lua?
[+] swah|12 years ago|reply
...and it involves table-based programming.
[+] copx|12 years ago|reply
Some things to add:

* Lexical scoping * Tail-call optimization * First class functions / closures

Lua is not a functional programming language but I tend to have a large number of closures and recursive functions in my Lua code. Lua's limited feature set means that you end up using closures as a replacement for things which are special features in other language.

Here is an example of a closure doing the job of C/C++'s function-scope static:

  View.Move = (function()
    local move = {
      [Direction.up]      = View.Up,
      [Direction.left]    = View.Left,
      [Direction.right]   = View.Right,
      [Direction.down]    = View.Down
    }

    return function(self, direction) move[direction](self) end
  end)()
Note that a closure is more efficient than function-scope static here. The static requires a "is initialized?" check every time the function gets called, the closure does not.
[+] Derbasti|12 years ago|reply
Lua's function definition syntax is really the best one I have seen yet. One simple statement that combines all the flexibility of lambda and def (in Ruby/Python-speak). I wish Ruby and Python had a powerful statement like that.
[+] vor_|12 years ago|reply
One problem I have with Lua is that it silently returns nil when accessing non-existent elements. There are a few hacks around this, but the most common only protect global access. I have to wonder why the language reports no error in that situation given that it does report an error when comparing values of different types (no automatic conversion) or when attempting to index nil.
[+] copx|12 years ago|reply
It is the norm (for larger projects) to use a "strict" script which prohibits the use of undeclared globals. As far as local nil access is concerned, it is necessary for idiomatic Lua.

For example, this is how you implement optional arguments and default values in Lua:

  NeumannNeighborhood = function(position, extent)
    extent = extent or 1
    ...
  end
Here extent is optional and its default value is 1. That's how you keep a language small.
[+] justincormack|12 years ago|reply
Add a metatable to the tables where undefined access must not be allowed.

a = setmetatable({...}, {__index = function(i) error "undefined" end})

is all you need.

[+] kemayo|12 years ago|reply
It's because accessing a variable means looking up a name in the global environment, which is technically just a table. Since it's just a table like any other, any non-set key has nil as its value.

You can actually access said global environment as the _G variable.

Now, you could totally stick a metatable on _G that caused some sort of exception when you tried to get the value for a key with a nil value.

[+] gruseom|12 years ago|reply
it silently returns nil when accessing non-existent elements

Is that similar to what JS does with undefined, or am I missing something?

[+] pixie_|12 years ago|reply
Does that make it like Objective-C because I love how accessing a null object doesn't blow up the application. So many trivial bugs in my iOS app that would have caused the entire application to crash are totally avoided. Unless you're creating software for the space shuttle, avoiding null exceptions entirely is a god send for my users.
[+] thinkpad20|12 years ago|reply
Big fan of Lua. I have only poked around at the language, and never really done anything significant with it, but I love how simple and powerful it is. Every time I break out the lua programming book I wish I were using that at work instead of python. I don't agree with every choice they've made, but it's a great language for sure.
[+] copx|12 years ago|reply
Years ago I read a Lua tutorial but I didn't like the language much.

Nowadays I am working on a large 99% Lua code base and I love the language! Lua has a small set of building blocks and only if you try to write something non-trivial in Lua you realize how incredibly well these blocks fit together.

It's funny, Lua is the opposite of Go for me there. I was excited about Go after reading the tutorial, but then I tried to seriously use it the experience was underwhelming.

[+] PavlovsCat|12 years ago|reply
Same here: I once wrote a bunch of simple plugins for Renoise in LUA which was my first (and so far last) contact with it.. I forget everything about it since then, other than that it was very fun! I spent time learning the Renoise API and figuring out UI stuff, LUA itself just kinda worked. It was like jumping into cold water and finding it rather warm and pleasant.
[+] jzwinck|12 years ago|reply
What do you dislike about Python for work?
[+] sker|12 years ago|reply
The thing is indeed fast. Looking at these benchmarks[1], it always makes it to the top 10 on the tests where it appears.

Edit: Apparently, OpenResty[2] is largely responsible for the impressive performance in those benchmarks, and Lua may be secondary.

1. http://www.techempower.com/benchmarks/#section=data-r6&hw=i7...

2. http://openresty.org/

[+] bhauer|12 years ago|reply
Lua is quick. OpenResty and nginx are certainly providing a very high performance ceiling, but the test is indeed Lua code [1]. Many test implementations for other dynamic languages are similarly provided a high ceiling by nginx but then fail to deliver on that potential.

[1] https://github.com/TechEmpower/FrameworkBenchmarks/blob/mast...

[+] rsiqueira|12 years ago|reply
I love the fact that Redis database can run Lua scripts, allowing very sofisticated queries.
[+] clumsysmurf|12 years ago|reply
For the JVM there is a native (no JNI) Lua port called luaj:

http://luaj.sourceforge.net/

I would be interested to hear anyone's experience using luaj on Android (thats where I plan to use it), and how it may compare to other options like Rhino.

[+] dottrap|12 years ago|reply
I never got past initial investigation of kahlua and luaj. However, they were appealing because I had a large server application based in Java that utilized spawning off other processes to help map/reduce data. We had written Lua scripts to do these helper tasks. But because of the Java, it was very expensive to spawn off other tasks, because they also needed to be wrapped in Java and starting a new Java instance ate a lot of memory and had a slow start up.

The idea was to use kahlua or luaj to do the same helper tasks within the main process so other Java instances wouldn't have to be spawned.

But for Android, plain old C based Lua works great with the NDK. There is also LuaJava and JNLua which can cross the native/Java bridge.

[+] DigitalSea|12 years ago|reply
I love Lua, it's definitely underrated. My first experience using Lua was via Indigo Rose Setup Factory, it uses Lua for scripting installer logic and the thing I love most is how easy it is to learn (even easier than a language like PHP). A few hours and you've grasped most basic concepts and conventions, if you already know another language you'll find the learning curve further reduced once more.
[+] frik|12 years ago|reply
Does someone use Lua for system scripting? Replacing bash and perl script on the server looks like a good fit. If so, can you share your experience?
[+] chalst|12 years ago|reply
It's used for some configuration/ scripting in NetBSD, after Marc Balmer pushed the idea a few years back.

Some links:

- The Lunatik GSoC project - http://netbsd-soc.sourceforge.net/projects/luakern

- Marc Balmer's FOSDEM'13 paper - http://www.netbsd.org/~mbalmer/lua/lua_config.pdf

I gather that the principal virtues of Lua for this purpose are:

1. Lua tables have a nice syntax

2. Lua's load function is very fast

3. Lua has a small, well-tested trusted code base

4. It has a sane semantics for dynamic loading

[+] omaranto|12 years ago|reply
You'd need some libraries before that was comfortable. At the very least you would want to install LFS (Lua File System), for things like checking if files exists and what permissions they have. If Lua string patterns don't cut it for your text munging needs, you would want another library, either a regex library, or, even better LPeg. Even with those things, things like running several commands connected by pipes is not going to be as convenient as in sh or even Perl.
[+] cageface|12 years ago|reply
If I needed an embedded scripting language for a project I'd reach for Lua without a second thought.

I've been strongly tempted to use it for actual core app development but every time I look at it the scarcity of common libraries compared to something like Ruby or Python cools me off.

[+] malkia|12 years ago|reply
coroutines in lua a great for game development. Also the reference, luajit and one more commercial that I've seen are all based on Lua state object, rather than global data (python?).

And luajit is more alien technology than common lisp :)

[+] da02|12 years ago|reply
When you say "alien tech", do you mean "highly advanced" or "very different/hard to understand"?
[+] ANTSANTS|12 years ago|reply
The title is misleading. I was expecting some kind of discussion of Lua's implementation, not yet another list of reasons why Lua is cool.
[+] ulisesrmzroche|12 years ago|reply
I remember people used Lua to write WoW mods, but that's about it. Anything significant written in Lua?
[+] compay|12 years ago|reply
Adobe Lightroom is about 40% written in Lua (the stat comes from Wikipedia). IIRC C and C++ are used for image processing and compute-intensive stuff, but the rest of it is all Lua. I believe that would be the most widely used app written mostly in Lua, though Apache, Redis, Nginx, Vim and some other big projects support using it for scripting.
[+] Elv13|12 years ago|reply
VLC, MySQL (and MySQL Workbench), Wireshark, AwesomeWM, NMAP, NGINX, Ogre, CEGUI (and most corporate game engines), OpenWRT, Recent TI calculator (bye bye TI basic)

Many more, but it is what come to my mind right now

[+] stevekemp|12 years ago|reply
I've been working on a console-mail client which is configured and scripted entirely in Lua:

http://lumail.org/

While I cannot pretend it is significant, it is new/fresh/recent. I'm just going through a transition to the GMime mime library, but beyond that the code is stable and I've used the client for the past few month or so, replacing mutt as my main client.

Now that I'm using it I'm finding interesting things to do all the time, using Lua has allowed me to move from having a mostly-extensible client (mutt) to having a __very__ extensible client.

[+] copx|12 years ago|reply
Lua is an industry standard in the video games business. The scripting language used is usually Lua or Python. Examples of Lua-scripted games: Civilization V, King's Bounty, SimCity 4, Dawn of War, Crysis etc.
[+] marshray|12 years ago|reply
A lot of WoW itself seems to be written in Lua. A bug in Lua would occasionally allow a cheats in WoW, so it got heavily tested by motivated folks. I'd call that a significant deployment.
[+] Kiro|12 years ago|reply
You use Lua to write cross-platform apps in Corona.
[+] microcolonel|12 years ago|reply
I prefer scheme for an extension and wrapping language, but I can see how LUA's paradigm could be the fisher price buttons non-coders need.
[+] snogglethorpe|12 years ago|reply
Scheme's a very decent language too, but so far I haven't found any scheme implementations out there which are anywhere near as good as Lua—small, portable, highly efficient, robust, easy to integrate [with the "underlying" program], well maintained, good and active community, etc—for this space. The most popular scheme implementations tend to be enormous, despite the language's historical reputation as a "small" language.

Lua's a great language, but the amazingly good implementation is as much (or more) a factor than the language itself.

[+] kazagistar|12 years ago|reply
Scheme is fine if your goal is only to have a small section of the programming community want to use it. Lua is great for everyone, including people who have never coded before in their lives as well as experienced software developers.
[+] oakaz|12 years ago|reply
I wrote some Lua couple of years ago but can't see any reason to use it since from that time.