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.
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.