Programming in Lua - Roberto Ierusalimschy

Hot on the trail of me reading up on Forth, I've been reading up on Lua. Now, this is a language I could actually see myself using. Which is convenient, as odds are that we may well use it for a project at work.

First off, the language: It's actually pretty neat. The syntax is a little odd, as it has no statement separator - it just kinda works out where the statements go! It has proper closures, but after a language like ML or Haskell they feel pretty heavy syntactically, even though they're not really. I've been spoiled. Actually, it not only does lexical scoping, but it provides for a form of dynamic scoping. Nutty. It's a dynamically-typed language, but it feels like a language where this is justified, so my normal dynamic-language-hate doesn't really kick in. The core language itself is small, and the implementation is very small too, on the order of 10k lines of C to implement something industrial-strength and pretty efficient. The libraries are nicely designed, too.

In all, it's very, very competent. It falls just short of super-neat. It feels like it should have some kind of weirdo extensible-parsing/macro thing like Lisp or Forth, but it doesn't. It almost, but doesn't quite make a particularly pleasant way of writing functional programs. I still want something of a type system, but I suspect that's just force of habit. It does have a kinda roll-your-own support for objects. It sits in the space occupied by Perl, Ruby, Python and that lot, and apart from the text processing, I think I rather prefer it, as it's such a compact and, as I said competent language.

The real killer feature, though, is that it's a specifically embedded language. It's designed to be tacked into another system to script it. You can call it from C, and it can call C functions registered with it. The C API is pretty neat - it feels as if as much design has gone into that as into the language itself. A C++ interface would make things a lot neater in places, but given no-one can agree on which subset of C++ to use, and so much open source software is written in vanilla C, and you can understand where they're going. The one thing that's rather disappointing on the C++ interaction front is the error-handling model, as it's setjmp/longjmp based, which is not guaranteed to play nicely with C++ exceptions. Ho hum.

Anyway, that's Lua-the-language. What about this book? It's written by the language's principle author. It's a self-published book, for whatever reason they chose, but it's very well done - you'd hardly suspect! :p I find books about languages by the languages' authors to be very mixed. I certainly never got on with the Camel book, for example. This is right up towards the top end. It's not the Lua Reference, and it's very clear on that point, but despite this it feels like it's providing a very comprehensive view of the language. It's well-structured and well-organised. I learnt well reading from it, and can find things easily. All-in-all, it really isn't the Camel book, for which I am most glad. Mr. Wall could certainly learn a thing or two here.

This is the paper edition of the book (second edition). The first edition is available online, and I read the start of that. This version is only mildly better, but I would still recommend getting it, as you get a pretty book and a warm feeling that you're supporting people write non-sucky open source software.

Posted 2010-09-25.