from Hacker News

Lua: Good, bad, and ugly parts

by adamansky on 3/9/13, 2:51 PM with 110 comments

  • by graue on 3/9/13, 5:05 PM

    Lua's great. I recently used it to rewrite a small audio effect framework[1] based on an older project of mine in C[2]. To my astonishment, without any optimization efforts on my part, the LuaJIT version of one effect ran faster than the C version, while another more complicated effect (a filter) only took 2.01x as much time as C. And this was with simple, clean, mostly declarative code (read it on GitHub and see what you think).

    One of the goals of this project was to compile effects code to JavaScript with Lua.js[3] and produce a demo that ran in the browser. There, unfortunately, I ran into a showstopper with a Lua.js bug[4] that breaks my approach to creating modules. Unlike LuaJIT and regular Lua, Lua.js is an experimental project and far less mature - though totally awesome. I might make another attempt to fix the problem myself at some point. With a more mature Lua.js, you could write fast Lua code and port it to nearly every environment.

    [1] https://github.com/graue/luasynth

    [2] https://github.com/graue/synth

    [3] https://github.com/mherkender/lua.js

    [4] https://github.com/mherkender/lua.js/issues/13#issuecomment-...

  • by ethereal on 3/9/13, 5:18 PM

    I've been using Lua in a project for quite some time, replacing Python as my embedded scripting language of choice.

    It never ceases to amaze me, as a language and as an interpreter. Sure, the syntax bites you occasionally, and sure, the lack of built-in functionality is occasionally annoying. But hey, I can add callbacks into a simulation engine that execute 10 million times per second with almost no slowdown in the result. Like the article mentions, the string matching library isn't as powerful as Perl's, but it's more than adequate for all of my uses.

    My only wish is for an updated LNUM patch that works with Lua 5.2. I deal with integers too much in my projects for its absence to not be annoying.

  • by andrew-d on 3/9/13, 4:46 PM

    I recently started programming in Lua too - it's a fantastic language if you want to embed it in anything, and using the LuaJIT FFI [0] makes interfacing with C code from pure Lua a pleasure. I've also started writing a set of extension libraries[1] that provide various "missing" features - mostly inspired by Ruby.

    [0]: http://luajit.org/ext_ffi.html [1]: http://andrew-d.github.com/lua-ext/

  • by peterfschaadt on 3/9/13, 9:53 PM

    It's very easy to extend Nginx with Lua [1] to create a custom Nginx setup for fast API endpoint authentication, diverting beta users, or handling sockets. I'm hoping to see Lua continue to grow and solve some of its shortcomings like unicode support and pattern matching.

    [1] https://github.com/chaoslawful/lua-nginx-module

  • by shaneeb on 3/9/13, 4:21 PM

    Haven't used Lua much but the concept of tables is really neat. Its simple, orthogonal and the fact it lies at the core of many features in Lua (composite data types, metaprogramming, etc) means those features are neat too.
  • by saosebastiao on 3/9/13, 4:46 PM

    I'm sitting here wishing lua was the de facto web language. Javascript makes me sad.
  • by phaedryx on 3/9/13, 5:03 PM

    I've been looking for a way to introduce my children to programming.

    I've been considering several different languages to start them on and Lua has turned out to be a great choice. My children both love minecraft and we've been writing programs in Lua for the computercraft mod: http://www.computercraft.info/

  • by greggman on 3/9/13, 5:48 PM

    Having never used a language that indexes from 1 I'd expect that would make a ton of common math used in programming either break or be more complicated.

    How is it in practice?

  • by vor_ on 3/9/13, 7:39 PM

    Here's an article I've submitted before that makes a case for considering Squirrel instead of Lua:

    http://computerscomputing.wordpress.com/2013/02/18/lua-and-s...

    The best reason against using Lua in my mind is that undefined variables in Lua return nil, which can lead to typo bugs and therefore unintentional sparse arrays, which screws up the result returned by the length operator. It also has inconsistent boolean expression evaluation:

      while 0 do print("Loops forever") end
      while not 1 do print("Does nothing") end
      while 1 do print("Loops forever") end
      while not 0 do print("Does nothing") end
    
    It's nice having real classes in Squirrel with type introspection and corresponding C APIs. In Lua, I have to juggle metatables in the registry to construct a class system, and my system will differ slightly from someone else's. Squirrel's other niceties like compile-time constants, 0-based arrays, and automatic reference counting for predictable memory management overhead make it a quite nice alternative to Lua.

    It sounds like I'm bashing Lua here, though I still find it fun to program occasionally. But I do wonder how long Lua can remain as popular as it is with an unconventional syntax, inconsistent behavior, and a minimal built-in library in the face of richer alternatives like Squirrel, mruby, and even Tcl, which has improved much in recent years. There must have been a reason Valve Software chose to use Squirrel in L4D2 and Portal 2 instead of using Lua as most apparently do.

  • by johnmo on 3/9/13, 5:31 PM

    > The "hash" part doesn't have a defined length. Both parts can be iterated over using the pairs method, which allows you to count the number of elements in them

    While true, you should iterate integer-key tables using ipairs and hash tables using pairs.

    > No continue statement

    Continue doesn't exist? I swear I've used it in Lua 5.1.

  • by yeureka on 3/9/13, 5:44 PM

    I have been using Lua to script the audio sequencer on the game I am writing and it rocks. No performance problems at all, even on an second generation iPod Touch. And it is very easy to call Lua from C++ and C++ from Lua.
  • by gruseom on 3/9/13, 5:11 PM

    It's neat to see Lua building up to critical mass based on just plain how good it is. What will be the killer app?
  • by xtremejames183 on 3/9/13, 10:30 PM

    There is now a serious alternative to lua: Jx9 which uses a syntax similar to C and JavaScript and is being used in many commercial games. http://jx9.symisc.net/