Good article, but one nitpick: not everything in Lua is a table. Tables are a versatile data structure that can be used as arrays, dictionaries, objects, etc. However, Lua provides many types besides tables: numbers, booleans, closures (functions), strings (which are interned), coroutines, and something called userdata (which are typically pointers to something implemented in C). Another cool thing about Lua, which w…
Why Lua?
41–50 of 142 posts
Re: Why Lua?
#42Earlier quoted context omitted.
Could you explain what you can accomplish with Lua metatables that you can't do with python __eg__ methods? I've only seen them used as a way to implement operator overloading.
__index and __newindex are the key ones - effectively equivalent to method_missing in Ruby. __index gets called if you try to read a non-existent key, and __newindex gets called if you try to write to a non-existing key. You can use these methods to implement inheritance, proxies, getters/setters, all sorts of stuff.
[0] http://docs.python.org/reference/datamodel.html#object.__get...
Re: Why Lua?
#43To echo chubot: Why isn't Lua more widely used? * It lacks an easy-to-use symbolic debugger. * It lacks a first-rate IDE. * It lacks a standard way for people coming from OO/Java to define objects and interfaces. * It lacks a GUI toolkit. * It has a great set of manuals. It lacks an O'Reilly book with a woodcut animal on the cover. * Arrays indexes start at 1. Except for the last item, these are all relatively small…
Lua's goal is to be no-fat and customizable to only what you need for your unique situation. It's even common practice for systems to cut out stuff like the standard library file I/O module if it's not appropriate for your situation.
Re: Why Lua?
#44I've used Lua for a couple of personal projects. My main objection to it is that accessing an undefined variable or member returns nil rather than throwing an exception. If you make a typo, you don't find out until you try to call or perform arithmetic on your nil value. Since inserting nil into a table just deletes that key of the table, the use site might be several steps away from the typo. Other than that, I agre…
function nilguard(tbl)
tbl = tbl or {}
local mt = {
__index = function(t,k)
error("Invalid key: " .. tostring(tbl) .. "[" .. k .. "]")
end
}
setmetatable(tbl, mt)
return tbl
end
local myObj = nilguard({foo=10})
myObj.crap -- should raise
For undefined global variables, just require("strict")Re: Why Lua?
#45To echo chubot: Why isn't Lua more widely used? * It lacks an easy-to-use symbolic debugger. * It lacks a first-rate IDE. * It lacks a standard way for people coming from OO/Java to define objects and interfaces. * It lacks a GUI toolkit. * It has a great set of manuals. It lacks an O'Reilly book with a woodcut animal on the cover. * Arrays indexes start at 1. Except for the last item, these are all relatively small…
Lua's original focus as an embedded language has been a huge handicap here. There's almost no standardization, because each developer builds their own set of libraries based on their needs. It's sort of what server-side javascript looked like before node.js. Lua is in dire need of an opinionated killer platform. Unfortunately, unfamiliarity with the language (vis-a-vis javascript) means that the road to mass adoption…
Unfortunate especially given that Lua and JavaScript are extremely similar semantically. The difference between {} vs begin/end really does blind people to the all similarities.
Re: Why Lua?
#46I've used Lua for a couple of personal projects. My main objection to it is that accessing an undefined variable or member returns nil rather than throwing an exception. If you make a typo, you don't find out until you try to call or perform arithmetic on your nil value. Since inserting nil into a table just deletes that key of the table, the use site might be several steps away from the typo. Other than that, I agre…
setmetatable(_G, {__index=function(global_env, name) print("unknown: " .. name) end})Re: Why Lua?
#47Why isn't Lua more widely used? One reason is a consequence of it being an embedded language. Lua has had 5 major versions which are very incompatible with each other. You're just supposed to stick with the previous version until you upgrade your code. I read all the Lua papers, and they are quite fond of "getting it right" (which is refreshing). They will break things to get it right, whereas other languages stick w…
I think the major, overwhelming reason is because it has no standard library, much less a package-ecosystem such as RubyGems or Python eggs.
Re: Why Lua?
#48Earlier quoted context omitted.
Could you explain what you can accomplish with Lua metatables that you can't do with python __eg__ methods? I've only seen them used as a way to implement operator overloading.
Roll-your-own prototypical inheritance. I also vaguely recall rolling my own approximation to class inheritance long ago.
Re: Why Lua?
#49> Integration with C (and C++ for that matter)
Guile does it better. You can use shared memory threads in guile without any penalty. Atmost you have to allow for the garbage collector to run when inside FFI functions. But that is a small price to pay in case you need to use multiple parallel-concurrent threads with a single heap.
Guile was built with FFI in mind and has an impressive history. Just take a look at guile gnome bindings.
> Speed and Simplicity
Guile 2 is extremely fast. Not as fast as LuaJIT, but it no reason it won't get there. As for simplicity, take a look at the partial evaluator in trunk of guile 2.
> Education
Guile is good old scheme.
> Functional
Can't get more functional than scheme :)
> Everything is a Table
Well, almost everything is a pair in guile. Vectors and hash-tables are trivially available. Though I recommend to sticking to functional programming in scheme style.
> Consistent
As before, can't get more consistent than scheme.
> Portable
Guile is available on n900. So there.
To continue, guile has continuations (delimited or otherwise), and macros (hygienic or otherwise), both of which are effectively missing in lua.
And guile offers all of this while supporting native threading with a single heap. Sweeet.
Re: Why Lua?
#50Good article, but one nitpick: not everything in Lua is a table. Tables are a versatile data structure that can be used as arrays, dictionaries, objects, etc. However, Lua provides many types besides tables: numbers, booleans, closures (functions), strings (which are interned), coroutines, and something called userdata (which are typically pointers to something implemented in C). Another cool thing about Lua, which w…
To nitpick your nitpick: in Lua tables are the only composite data structure. Everything you listed is either atomic or opaque (you can hide composite datatypes in userdata, but you can't introspect the values). If you think about it, this is very similar to C where the only composite datatype is a struct (arrays are just sugar on pointer arithmetic). In fact, I think if you wanted to make a scriptable dialect of C,…