8 ms·
Lua: Good, bad, and ugly parts
- shaneeb 14y agoHaven'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.
- mscarborough 14y agoWhat do you mean by orthogonal in terms of simplicity and similarity to other Lua features?
- shaneeb 14y agoOrthogonal in the sense that the same concept is used if you need maps/arrays or for namespacing functions or metatables(for metaprogramming). Its like a "core" concept that binds seemingly different features in an coherent way. Havent seen a concept used this elegantly in any other language
- gruseom 14y agoLists in Lisp, objects in Smalltalk, arrays in APL, stacks in Forth. Lua keeps first-class company.
- Shorel 14y agoGo back to Linear Algebra class.
- ubercow13 14y agoThat was unnecessary. The meaning is the same.
- stcredzero 14y ago> Havent seen a concept used this elegantly in any other language Object in Smaltalk.
- vor_ 14y agoI find Lua's use of tables as a universal container to be clumsy. For example: local array = { one, two, threee, four } A gap is silently created in the array because Lua returns nil for undefined variables. This causes the (#) operator to an return incorrect result and can lead to subtle, difficult-to-find bugs in the program. Lua makes no distinction between nil and a non-existent element, so assigning nil to an array element will also create a gap, rather than shifting the keys of the subsequent elements downward: local array = { 1, 2, 3, 4 } array[2] = nil -- array is now { [1] = 1, [3] = 3, [4] = 4 }
- gruseom 14y agoYour second example is not "clumsy", but a consequence of how Lua's tables are simple and cleanly designed. It would be weird magic for the elements to suddenly shift downward. One can do that oneself if need be. Your first example has a point – perhaps undefined variables should error when accessed – but that's got nothing to do with tables.
- vor_ 14y agoArray keys shifting downward wouldn't be weird magic; it would be the expected behavior when removing an element from an array. Dismissing it as simply how Lua's tables are designed doesn't make it intuitive or expected. > Your first example has a point – perhaps undefined variables should error when accessed – but that's got nothing to do with tables. Well, of course it does. The point is that you can silently end up with array "holes" due to a typo, which you are supposed to avoid in Lua for a number of reasons, such as an the length operator returning an incorrect result. I don't understand why my previous comment is getting voted down for pointing this out.
- groovy2shoes 14y agoIf you want the array elements to shift into the gap, you want to use table.remove: local array = {1, 2, 3, 4} table.remove(array, 2) -- array is now { [1] = 1, [2] = 3, [3] = 4 }
- FreezerburnV 14y agoI agree about the tables. Personally, I find Lua to just be a more honest language, in a sense. Languages like Python or OCaml really just use tables under the hood for objects and what not (from what I understand), but Lua shows you that outright.
- judofyr 14y agoIt's really annoying when you're working with JSON though. I've used some Lua libraries (the OpenResty stack) where it's close to impossible to generate "[]" because there's no difference between arrays and hashes (and thus an empty table gets converted to "{}").
- justincormack 14y agoWell JSON does assume JavaScript semantics, so this type of issue crops up a lot with lots of languages. You should be able to define an arbitrary value that is used as [] by the library fairly easily, or a property of the table.
- ludwigvan 14y agoHow was your experence with OpenResty btw? It looks like a really interesting project.
- judofyr 14y agoI think it sorely needs a framework on top that handles and generates the nginx.conf. There's quite a bit "hacking" to get every piece to work with each other (e.g. to talk to a database in Lua it needs to be configured in nginx.conf). But all in all it's surprisingly effective.
- akavi 14y agoHow is this different from objects in javascript? (Tangentially, I don't agree that conflation of array and object is a good thing)
- andrew-d 14y agoI 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 http://luajit.org/ext_ffi.html [1]: http://andrew-d.github.com/lua-ext/ http://andrew-d.github.com/lua-ext/
- saosebastiao 14y agoI'm sitting here wishing lua was the de facto web language. Javascript makes me sad.
- graue 14y agoWell, you can write client-side web apps in Lua: https://github.com/mherkender/lua.js https://github.com/mherkender/lua.js Unfortunately, there is a bug in Lua.js and it doesn't support the preferred method of creating modules[1]. For this reason, I've yet to get a substantial size app written. I'd still like to go back and fix this myself eventually, but I was sinking a lot of time into trying to get it to work. [1] https://github.com/mherkender/lua.js/issues/13#issuecomment-9220080 https://github.com/mherkender/lua.js/issues/13#issuecomment-...
- shaneeb 14y agoI guess if we could replace it with Lua we would still be sad. How do you think Lua makes for a better language for web?
- phaedryx 14y agoCompare https://www.google.com/search?q=javascript+wtf https://www.google.com/search?q=javascript+wtf vs. https://www.google.com/search?q=lua+wtf https://www.google.com/search?q=lua+wtf
- shaneeb 14y agoIf that was the measure, maybe we could use a language nobody knows because it will pass this test with flying colors :)
- phaedryx 14y agoI should have been more clear. I'm not saying to compare the number of results, but, rather, the things that people complain about with each language. I've been teaching myself Lua recently and I've found it to be a nice, consistent, small language. Javascript is a fun language, but has some bad decisions built in: http://oreilly.com/javascript/excerpts/javascript-good-parts/awful-parts.html http://oreilly.com/javascript/excerpts/javascript-good-parts... and it can be really confusing at times: http://wtfjs.com/ http://wtfjs.com/
- phaedryx 14y agoI'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/ http://www.computercraft.info/
- NateLipscomb 14y agoI agree. I've been teaching my 7-year old son to code and Lua has been a great choice for him so far. After having him play around with Scratch and learn some javascript with Code Monster, we got him going with some game development in Lua with the Corona SDK. It's proven to be pretty intuitive for him and he's having a lot of fun. He was super stoked when we first put his "game" on an iPad and play around with it. I'd definitely recommend it to other parents looking for something to start their kids on.
- graue 14y agoLua'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 https://github.com/graue/luasynth [2] https://github.com/graue/synth https://github.com/graue/synth [3] https://github.com/mherkender/lua.js https://github.com/mherkender/lua.js [4] https://github.com/mherkender/lua.js/issues/13#issuecomment-9220080 https://github.com/mherkender/lua.js/issues/13#issuecomment-...
- azakai 14y agoI'm curious to do some performance comparisons, can you perhaps say how one would build and run your code to replicate those benchmark results? (Especially but not only the one you say was faster in LuaJIT?)
- graue 14y agoI documented the tests on my project log: https://scott.mn/projects/luasynth.html#filter_effect https://scott.mn/projects/luasynth.html#filter_effect In short, I ran C and Lua implementations of the same command, 6 times each, using the built-in "time" command in bash. Averaged the "user" time for each implementation and compared the results. As input, I used a 2m:38.040s long song, 44.1 KHz stereo, converted to a raw file with 32-bit float samples, and I redirected output to /dev/null. For the first benchmark[1], which was faster for LuaJIT, I actually messed up: I compared the "real" time rather than the user CPU time. But as I recall, the "user" times bore the same relationship, and there was very little deviation among the six trials. If you don't trust me, feel free to do a more rigorous test! It's worth mentioning that the effect for which LuaJIT beat C was a simple gain, so what was really being tested was a loop that multiplied a bunch of numbers by a (within the loop) constant. The second test in which Lua was a factor of two slower may be more representative. To build the code, clone the graue/synth and graue/luasynth repos on GitHub. synth includes build instructions, while Luasynth merely requires you to install LuaJIT and run the script. [1]: https://scott.mn/projects/luasynth.html#lua_beats_c https://scott.mn/projects/luasynth.html#lua_beats_c
- gruseom 14y agoIt's neat to see Lua building up to critical mass based on just plain how good it is. What will be the killer app?
- chas 14y agoIt is popular in video games already. http://en.wikipedia.org/wiki/Category:Lua-scripted_video_games http://en.wikipedia.org/wiki/Category:Lua-scripted_video_gam...
- ConstantineXVI 14y agoLua is it's own killer app. It's an excellent choice for embedding, both from a technical (small, pure ISO C codebase, no dependencies) and legal (MIT license) standpoint.
- ethereal 14y agoI'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.
- justincormack 14y agoLooks like Lua 5.3 will have an integer type.
- ominous_prime 14y agoOoh, that would be really nice. Is there a roadmap anywhere, or is this mailing list chatter?
- fab13n 14y agoRoberto did a keynote at the Lua workshop '12, where he detailled what keeps the Lua team busy. A video of it exists somewhere on the web. Half of it was about how to integrate IEEE doubles and 64 bits ints soundly in the language. There are some challenges left, but they're really trying to overcome them.
- bsg75 14y agohttps://www.youtube.com/user/luaworkshop https://www.youtube.com/user/luaworkshop
- johnmo 14y ago> 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.
- seany 14y agoipairs is gone in 5.2
- Dylan16807 14y agoThey changed ipairs around a couple times. It's currently in. Either way you should be using not-pairs. If you construct a table in a weird way you can end up with pairs going out-of-order. ipairs works fine, also "for i=1,#t" works fine.
- vor_ 14y agoThere's no continue. Instead, you must use goto. > While true, you should iterate integer-key tables using ipairs and hash tables using pairs. In my opinion, this makes Lua's combination of arrays and dictionaries pointless since one must distinguish between them anyway for iteration.
- Hinrik 14y agoDo you really consider that to be a drawback? How often do you find yourself wanting to iterate a datastructure without knowing whether it's an array or a hash?
- yeureka 14y agoI 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.
- greggman 14y agoHaving 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?
- ominous_prime 14y agoI never found it to be a big deal, unless you're trying to copy algorithms from another language nearly verbatim. There's not really any math that relies on 0 indexing,
- greggman 14y agoI guess it depends on what you mean by "relies" Using a single dimensional array as a 2 dimensional array relies on zero indexing. index = y * width + x array[index] Using modulo to covert an index back to 2 dimensions relies on 0 indexing x = index % width y = index / width Skipping a prefix in a string. prefix = "ABC" string = "ABCdef" unprefixed = string[prefix.length:] I'm sure there are tons of others. Yes, you can add 1 or subtract 1 in the right places. That's the annoying part of it though.
- Dylan16807 14y agoWell lua doesn't even have an operator for integer division.
- ominous_prime 14y agoGood points. I was thinking that there are probably an equal number of array arrithmatic tricks that also require a +-1 here and there, but it was just a hunch. As I said though, never found it to be a problem in practice.
- rbehrends 14y agoEh, for every use case where it's more convenient that arrays start at zero, there's one where it's more convenient that arrays start at one, and vice versa. For example, to get a prefix of length 'n', you need characters 0 through n-1 with zero-based indexing and 1 through n with one-based indexing. (That Python hides the -1 when using a [0:n] range is a convenience; the subtraction still happens internally.) Similarly, to get the last element of a one-based array with a[length(a)] and the last element of a zero-based array with a[length(a)-1]. What it comes down to is that sometimes you have to write +1 or -1 for zero-based indexing where you can avoid it for one-based indexing and vice versa. What is not the case is that it only universally happens for one of those schemes.
- vor_ 14y agoHere'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-squirrel-the-case-for-squirrel/ 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.
- Fargren 14y agoCorrect me if I'm wrong, but what's inconsistent there is not boolean evaluation, it's number to boolean conversion. 1 == true and 0 == false are pretty standard, but that doesn't mean 0 and 1 are booleans.
- vor_ 14y agoOnly nil and false evaluate to false. Because there's no distinction between nil and non-existent elements, checking the existence of an element with a value of 0 requires that 0 evaluates to true, so you get this situation: while 0 do print("loops forever") end while not 0 do print("does nothing") end
- 14y ago
- peterfschaadt 14y agoIt'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 https://github.com/chaoslawful/lua-nginx-module
- xtremejames183 14y agoThere 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/ http://jx9.symisc.net/
- Jare 14y agoIt looks neat, although the use of $ turns me off about as much as the 1-indexing in Lua. :) Do you have a list of released games that use it?
- xtremejames183 14y agohttp://cplus.about.com/b/2013/01/27/what-is-jx9.htm http://cplus.about.com/b/2013/01/27/what-is-jx9.htm