92 points · by shawndumas · 14y ago · 68 comments
When I discovered Lua the first time I was enthusiastic. The JIT is so fast, and everything seems to be fine. But missing Unicode is a big disadvantage for Lua. Unicode is a must to have for modern languages. It makes things so much easier when you know you don't have to convert any fonts because they are all presentable in Unicode.
How nice Unicode is I always realize when I copy and paste some foreign text from somewhere into Emacs or Racket Scheme and everything works just fine.
1) Lua takes a page from the Scheme book in that the authors try to give a very minimal yet very extensible core -- they don't give you tons of features, but they try to give you the baseline constructs you need to build other features. It's easy, as it is in Scheme, to do procedural, object-oriented, and functional programming in Lua (and even mix them).
2) Lua syntax can be pretty (although I've found that it doesn't lend itself well to embedded DSLs).
3) Lua is distributed as a very small library with an intuitive (at least for me, YMMV) C API, which makes it trivial to embed.
4) The Lua VM is more performant than Python, Ruby, Perl, PHP, and Tcl (although you get far fewer libraries; no batteries included). There's also the LuaJIT implementation, which is the fastest dynamic language implementation I know of (and it's fully compatible with the stock C API).
5) Lua is straightforward to learn (at least as straightforward as Python or QBasic), yet has a lot to offer the more knowledgeable programmer.
6) The documentation is detailed, specific, and concise.
All of these points make Lua an excellent language for embedding into applications (which is what it was originally designed for), but it's not often an excellent choice for general-purpose programming or even general-purpose scripting. Point 1 is a double-edged sword, just like it is in Scheme: you often end up with many different object systems, module systems, etc. in different codebases which can be painful for interop. I hear that this is also a painpoint for Tcl.
It seems to me the outstanding feature of Lua's design history is its designers' determination to make the core ever simpler and more general. That takes vision and courage over a long period of time. What emerged is truly interesting and inspiring (much more so than cobbled languages that grow only by accretion of features) and is likely to grow in importance.
[1] http://www.inf.puc-rio.br/~roberto/talks/novelties-5.2.pdf
Lua is terribly impressive. Someone needs to make a transpiled language targeting Lua and JS. They're semantically close enough that this might not be too complex. If it worked well, it would be an interesting alternative to Node.js, since you could write the server-side for Lua coroutines and so on. Not to mention its performance.
However, the grammar rules are fairly convertible. I've converted a parser that normally targeted javascript, to target lua instead (it was for a project to convert lua code into json).
You could make it easy to take advantage of Lua-only features like metaprogramming by denoting them server-side only. You'd have to do that to get coroutine support anyway.
The idea seems so exciting and obvious that I wish I had time to do it. Lua doesn't seem to be used much for server apps, though. Why? Are there technical limitations that make it unsuitable, or is it just that it hasn't been exploited yet?
Another thing I'd like to know, given how embeddable Lua is, is whether one could embed it as a browser plugin and then get the power of Lua client-side as well. Obviously, one wouldn't demand this of users, but it would be a pretty sweet option to offer: "install this little plugin to make the program faster and more responsive".
One final thought. I wish Google had gone with Lua instead of making Dart. Lua is basically JS done right. Seems to me they could have built a compelling case around it.
j = 10 -- global variable
local i = 1 -- local variable
( from http://www.lua.org/pil/4.2.html )Same with functions. To declare a function that is restricted to the local scope, you must explicitly use "local":
local f = function (...)
...
end
Can any Lua aficionados explain why this is the case? Surely a better design would be for variables to have local scope by default, and to require an explicit "global" prefix to be globally visible? Does this ever cause problems in real-world code?Well, the first answer to why we have global by default is history. Lua started out as a data description where global by default was a convenience.
Local by default doesn't work so well either with nested scopes. Suppose we do have local by default and then consider this code:
x = 0
function a()
x = 1
function b()
print(x)
end
end
It is impossible to refer to a's x variable. This makes it harder to return a simple counter function, for example. This is regular Lua: function create_counter(initial_val)
local count = initial_val or 0
local print_count = function() print (count) end
local increment = function() count = count + 1 end
return print_count, increment
end
Every time you call make_counter() you get a new pair of functions with their own internal count variable.See also PEP 3104 for more: http://www.python.org/dev/peps/pep-3104/
Every time you call make_counter() ...
create_counter()If, local was the default, you would have:
function create_counter(initial_val)
count = initial_val or 0
print_count = function() print (global count) end
increment = function() global count = global count + 1 end
return print_count, increment
end
Something like that, it doesn't seem that much worse. Also, why wouldn't the print_count function inherit the scope of the containing context so you wouldn't even have to have the "global" keyword. function create_counter(initial_val)
count = initial_val or 0
print_count = function() print (count) end
increment = function() count = count + 1 end
return print_count, increment
end ... create_counter() ...
That's what I get for writing code on a small phone screen. :-) function create_counter(initial_val)
count = initial_val or 0
print_count = function() print (global count) end
increment = function() global count = global count + 1 end
return print_count, increment
end
You might not want to use 'global', because that implies global scope. And in this case, this is not what was intended. For Python, keywords like 'outer' or 'nonlocal' were proposed.All in all, I'd be OK with 'nothing by default', but I'm also OK with global by default as it is now. If accidental creation of globals is a problem I can use the 'strict' module.
function create_counter(initial_val)
count = initial_val or 0
print_count = function() print (count) end
increment = function() count = count + 1 end
return print_count, increment
end
If local was the default, wouldn't the assignment to count in increment() create a new local variable (which is then discarded)?Essentially, instead of asking "why global by default", you should have to explain "why local by default is a good idea". In practice, I've found it something easy enough to get over. You don't have to believe me, though...give the Lua Missions a try and see if you don't like Lua just a bit more than you thought you might: https://github.com/kikito/lua_missions
Io - another minimal language - avoids the issue by having different operators for definition (:=) and assignment (=).
Hopefully by now language designers realise that anything-by-default is wrong. (That's right, I'm looking at you, Python.)
Consistent perf in video games is crucial and the Lua garbage collector can be brutal in this regard. Newer versions of Lua support incremental garbage collection but it's quite realistic to dedicate 2-3 milliseconds of every frame to garbage collection. Yuck. (16ms = 60fps, 33ms = 30fps)
Lua is trivial to implement into an existing engine. Lots of iOS engines heavily rely on Lua. Large games devs don't want to become reliant on those middleware engines. It can take them many months to update to latest iOS sdk. It took several popular engines 6+ months to integrate the in-app purchase SDK update.
Havok, owned by Intel, purchased Kore and turned it into Havok Script. Havok Script is a virtual machine that runs a "Lua compatible" language. It is extremely fast. Ridiculously faster than straight Lua. It also has full debugging support and you can trivially step through a call stack that bounces between C++ and script. Havok middleware is not known for being cheap.
That said, I friggin love Lua for its flexibility and transparency. Once you grok its few basic mechanisms, everything else just falls in place. In contrast, understanding Ruby or Python is a lot more difficult since the languages themselves are a lot more complex.
In fact, if I were to teach programming, I would teach it using Lua. Implementing your own object system is increadibly instructive. Learning about coroutines and lambdas is, too. And Lua is still small enough to just read its source when in doubt. It's bytecode consists of just a few dozen instructions.
Honestly, Lua is a marvellous language and I just love to use it!
If these things appeal to you check out lisp (clojure is my personal recommendation)
Lisp is built around cons cells and the linked list. But it has seperate containers of various types.
Lua is built around the Lua table, which can function as a list, array, and dictionary/hash table. You can then build any other data structure from tables easily. With a metatable, you then also get read-only tables, objects and classes, and more.
Now if someone were to design a modern Lisp based around tables...
Instead, it has seemed more like simply stepping from a clean, compact room into a large, similarly clean and smooth room but with depth you hadn't imagined before.
Well, that and learning Emacs compared to ST2 is a bit of a step at the same time.
I wanted to start out using Lua as a scripting language which may not be a good fit, but it seemed more compelling than other languages I was looking into.
To what extent does its easy C interop make up for this?
It should probably be noted that Lua gets along less well with C++, so that glue is a bit harder to write for C++ libs. There are tools that will automatically generate that glue for C++, but I'm not familiar with them.
LuaJIT2's ffi also makes it a lot quicker.
One thing worth knowing though: he's working on the 3rd edition (updated for Lua 5.2), though I'm not sure how soon that is expected.
The first edition is online, if you want to get an idea what it's like: http://www.lua.org/pil. In addition, the language's refrence manual is freely available online as well. Depending on how you learn, that would get you started very quickly. (It's a small language and very much follows the principle of least surprise - except for indexing arrays from 1, but you get used to that.) http://www.lua.org/docs.html has the full reference manual for 5.1 and 5.2.
To be honest, I learned it using only the tutorial here: http://lua-users.org/wiki/LuaTutorial and google.
You can embed yourself lua, and that's it's power. It can go down to 30-40kb (without parser), and run on PS3 SPU chip (256kb memory).
It does not use any globals, all goes through a state structure, so you can have multiple runtimes (and different ones, HavokVM, lua reference, luajit) if you want at the same time.
Don't get me wrong, Corona is cool, but there is also moai, and tons of other toolkits, and hundreths of other games that are using it - for scripting, ui layout, configuration, many other things.
Computercraft, a mod for minecraft embeds lua in minecraft for you to program. Not a direct use of lua in gaming, but cute!
Many other games use it for scripting. When I worked at Sony, we were using it on quite a few PlayStation games.