It’s been almost a year since my last post. I’ve been journaling mostly offline for a while. I’ve been trying to write 1000 words a day each day. A lot of the time I achieve that, some days I don’t. But I haven’t been good at going back through my writing to pull out things and put them up here on the blog. I’d like to start doing a better job of that.

Thoughts on Lua

I’ve been messing with Lua for the past week or so. I’ve been fairly successfully using it to go through the challenges on code eval (sea6ear on the leaderboard).

So far I really like Lua. I’m kind of a sucker for simplicity and performance, and the abstraction abilities of Lua are pretty good for a language that isn’t Scheme, Common Lisp or Haskell.

However, I’ve used it enough recently that I’ve started to see what are probably the most obvious annoyances. [note: I’ve only been using it steadily for a week, I’m still a newbie. these are mostly first impressions.] Also these are more gotchas than showstoppers, I think once you hit these and are aware of them, you can avoid them.

The most obvious annoyance once you start writing code is the need to explicitly declare local variables. It’s easy to forget and end up with global variables when you didn’t intend for them to be. This is pretty much the same way that JavaScript or Perl handle things, so this isn’t unique to Lua.

A more important annoyance is the interaction between two features that don’t seem dangerous in themselves. Lua automatically coerces values between numbers and strings. usually that’s something I find convenient. However, Lua’s tables also switch between acting like an array and acting like an associative array (dictionary/map/hash etc) based on whether integer indexing or string based (or other value) indexing is used.

What this means is that you have to be very careful when dealing with data that could be either a string or number and may be used as an index of a table. If you accidentally use it in the wrong format, or accidentally switch between trying to index with an integer vs string format, you will put or try to access data in the wrong part of the table. This can make it look like your data has disappeared when really it’s just been shuffled off to another part of the table that you weren’t trying to use right now.

On a different topic, and really just a minor annoyance, arrays are 1-based instead of 0-based. One based array indexing wouldn’t really be an annoyance except that the rest of the computer world has standardized on zero based indexing (except for awk, a language with some similar goals to lua). It’s not too bad in practice, except when you need to be translating between a zero-based scheme and the one-based scheme.

Overall I really like lua. I’m really impressed with it. It seems to have almost everything that I want to have in a language as far as semantics. I like that it has tail-call optimization so well constructed recursive functions don’t have to blow the stack. I like that functions can be passed as values. I like (overall) that tables are the central data structure and that you can use them to build all the other kinds of data structures that you desire. I think I will enjoy it’s lightweight approach to object orientation.

I wish that it was more evidently useful as a general class scripting language instead of being primarily thought of as a language for embedded scripting. On the other hand, I don’t want to change it so that it looses that focus. What I’d like to see is a real secondary distribution that had all the libraries to allow Lua to function in cross-platform general purpose fashion. (luarocks might be that, but I’m not sure how comprehensive it is or how widely it’s used. luadist seems like a similar attempt) so basically, I’d like the core distribution to stay small and focused, but there to be some kind of additional library or platform that would allow one to get all the libraries on needed for doing stand-alone work with lua.

I’m not sure how much work that would be. I’d like to see that happen. I want that enough that I’m willing to work to try to make that happen.

On the other hand, it looks like other people have occasionally tried similar things, and it doesn’t look like any of the other attempts that I saw (outside of luarocks, and maybe luadist?) have gotten persistent traction. Part of that may be that each other attempt at bundling additional functionality has been focused on some fairly narrow niche. I don’t know how much work it would be to create a curated library of various modules that would give you the basics you would need to do real work on various platforms. I’d like it to be something that you would download in addition to the standard lua download rather than instead of.

I’m not sure what kinds of additional functionality (outside the lua core) would be necessary for lua to be able to easily handle the same kinds of tasks that one would do in Python, Ruby or Perl.

Here’s what I can think of:

  • gui bindings
  • file system manipulation
  • http request libraries
  • web server capabilities
  • process interaction and control

Many of these exist in some form currently, but they are scattered and I’m unsure of the level of documentation or consistency of maintenance.

I wonder how many people focused on making it happen it would take to make Lua into a viable language for general purpose computing. I think it could probably be done with less than 20.

I think if I was involved in starting a company I would be tempted to use Lua for the scripting level (along with c for low-level, and erlang for very high-level tasks) in hopes that the company could contribute code to making Lua a viable language for general use outside the game industry and niche areas. Even though lua wouldn’t be as useful as python or ruby starting out, I think it would be worthwhile to focus on it and make libraries that did enable it to be used in a wide range of areas.

Lua could also use some more beginner oriented books. Programming in Lua is awesome, but it’s a pretty advanced text in terms of the language concepts it deals with. I would be interested in writing one of those beginner books once I am more familiar with it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s