I recently merged my first feature on a Lua project, so first, YAY! And now, I want to talk about why Lua is weird!
Wait, what’s Lua?
Lua is a scripting language that seems to be popular in gaming circles (World of Warcraft and Angry Birds are among users). I’ve also heard it’s popular to use for Minecraft.
Lua is:
- fast
- easy to run
- super small (1.1M uncompressed)
But why is it weird?
The most interesting thing I learned this week is that Lua has one data structure in its eight basic types: nil, boolean, number, string, function, userdata, thread, and table.
No arrays, no dictionaries, no objects, no candy, just one compound data structure: a table.
What is a table in this case? It’s not that different from a JavaScript object or an associative array:
local empty_table = {}
local my_var = { somekey = "somevalue" }
Assignment is done with =
, multiple values are separated by commas. This gets fun when you want to represent an array in Lua, and by “fun” I mean “disturbingly simple considering this is not in fact an array structure”:
local my_data = { "one", "two", "three" }
This is a table where the keys/indexes are integers. Interesting! Also interesting: they are 1-indexed and not zero-indexed.
Lua is prototypical like Javascript. But I think with fewer quirk 🙂 IMHO.