6 ms·
For those who are not familiar with Lua, or who have heard the prevalent "conventional wisdom" about it, here's a quick clarification: A. Lua's source code is
by _vya7 12y ago
For those who are not familiar with Lua, or who have heard the prevalent "conventional wisdom" about it, here's a quick clarification:
A. Lua's source code is really really small and extremely well-commented[1].
B. Lua is super easy to integrate... you can literally just throw Lua's entire source code into your app[2] and it'll compile just fine.
C. Lua's C API is super easy to use, and way more thought-out than Python's or Ruby's (no offense to Matz or Guido) -- yes, it is stack-based, but this actually makes its API much simpler and smaller than it would otherwise be. You can probably learn all of it in an afternoon (with the right guide).
D. Lua is uncannily similar to JavaScript in semantics, and the syntax[3] is ridiculously simple to learn. Semantically, it has way less magic to learn than either Ruby or Python.
E. Lua isn't only for scripting video games. I wish this "conventional wisdom" would just die. Yes, it is good for scripting video games, for the same reason it's excellent for scripting anything.
F. Table indices starting at 1 is not hard to reason about, not hard to use, and it's not hard to switch back-and-forth between Lua and languages where indices start at 0. This is a complete myth.
[1]: http://www.lua.org/source/5.2/ http://www.lua.org/source/5.2/
[2]: like this: https://github.com/sdegutis/mjolnir/tree/master/Mjolnir/lua https://github.com/sdegutis/mjolnir/tree/master/Mjolnir/lua
[3]: here's Lua's EBNF in its entirely, fitting on a single screen for me: http://www.lua.org/manual/5.2/manual.html#9 http://www.lua.org/manual/5.2/manual.html#9
- davidw 12y agoAll of the above are why antirez picked it for Redis, despite being a fan of Tcl. I've never used Lua, but it strikes me as 'the' thing in highly-embeddable niche these days.
- sdegutis 12y agoOh thanks for helping me remember the last bullet point: Lua is fine for small embedded environments, but it's also an excellent way to add scripting support to plain old bulky GUI apps like Firefox or Sublime Text or whathaveyou.
- alricb 12y agoRe: EBNF and C code, here's the parser for Lua 5.2: http://www.lua.org/source/5.2/lparser.c.html http://www.lua.org/source/5.2/lparser.c.html
- to3m 12y agoA contrary view, just to be fair and balanced. I really didn't enjoy using Lua, though it had its moments. A. Lua's source code is pretty inscrutable, and written in a style that was pretty alien to me. It was also a bit horrid to single-step through because of all those damn reference copying macros. But it's actually fairly easy to add stuff to it and modify it, and this was what actually convinced me to go with Lua in the end; look-mummy-my-first-Lua-hack was surprisingly easy to put in. B. Lack of variable declarations and global-by-default was a terrible idea. Languages should require declarations, or at least be local-by-default, and have a special interactive mode for when you want the no-declarations-and-global-by-default behaviour. Because making things "convenient" is fine, until it isn't, at which point it just becomes a bug magnet. For a language supposedly designed for use by people who don't know what they're doing, this borders on criminal; when you've got years of experience behind you, it's merely a huge pain in the arse. MaxScript gets this wrong too. C. 1-based indexing is annoying. If it makes no difference, switch to zero, because it's then the same as other languages; if it makes a big difference, switch to zero so that it isn't so difficult. Just because you can switch back from one way of doing things to the other doesn't make 1-based indexing right! MaxScript gets this wrong as well. But 1-based indexing would be wrong even if MaxScript and Lua didn't do it. D. Lack of a proper array type is silly; you have this ugly thing that's like an array, but not, and it's easy to stop it being like an array. Because it's not an array... it's a mapping. A mapping with some very odd ideas. Come on people, mappings and sequences aren't the same! Shape up! (At this point in my argument I used to be in the habit of adding, "after all, you wouldn't skip having separate integer and floating-point types, now, would you?" - because Lua famously had just the float variety. But these days they have integers too. So I'm still in the habit of adding it.) (The Lua guys' insistence on minimizing number of data types can also be seen in the lack of a separate symbol type. Even MaxScript doesn't get that wrong.) E. The C-side API is actually kind of weird, but on the plus side it's actually somewhat difficult to use improperly from C. So... I guess that's even. Maybe there shouldn't even be a point E, but... well. The C-side API really does stick in my mind. Aside from that, no complaints! (This post is at least partly just so that other people who don't like Lua know that they aren't alone.)
- ufo 12y agoVariables being global by default is definitely a wart. You can mitigate it a lot with a linter or with runtime tests (extending the global metatable). Local by default would be a bad idea and worse than global by default, IMO, as it would mess up lexical scoping. The ideal would be having a compilation error by default.
- hisham_hm 12y ago> Lua is uncannily similar to JavaScript in semantics I see what you mean, but before people run away screaming: the similarity lies more in the way it is amenable to Self-style prototype-based object orientation, and not in the various "wat"-style semantic quirks of JavaScript.