When you call up an element in a table in Lua, it's like accessing an array element - and it works well. But you can go further by associating a metatable
with a table - that's another table with a whole lot of extras - and that allows you to alter the behaviour of the original table. Since Lua works "by reference", you can apply the same metatable characteristics to a whole lot of different tables, resulting in what are different types of tables - very close to object orientation if you think of it like that.
To start you off, you can define a piece of code called __index
in your Lua metatable, and that piece of code will be run whenever you read
the value of variable that doesn't exist as a table member in its own right. You can also define a piece of code called __newindex
, and that piece of code will be run whenever you write
the value of a variable.
I've written a demonstration of this from first principles [here - full source]
. Let's look at that part by part:
Here is my __newindex method - I have simply added any input onto the end of a long single member called "sponge"
metapicture.__newindex = function(which,what,becomes)
which["sponge"] = which["sponge"] ..
what .. ": " .. becomes .. "\n"
Here is my __index method, in which I get ALL lines of the string that I've been building up that match the requested parameter, and return them as a single string
metapicture.__index = function(which,what)
rv = ""
for k,v in string.gmatch(which["sponge"],
if k == what then
rv = rv .. v .. "\n"
Basically, I'm implementing an "append anything" table.
I've tested that by storing several famous pictures in the single picture table, then extracting ALL the painters and ALL the picture names:
picture.name = "The Haywain"
picture.painter = "Constable"
picture.name = "Whistler's Mother"
picture.painter = "Whistler"
print ("\nNamed of pictures:")
print ("\nPainters of pictures:")
And running that, I see a list of picture names, then a list of painters, and not just the most recent picture of the most recent painter:
[trainee@holt lm10]$ lua index_123
Named of pictures:
Painters of pictures:
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesU107 - Object Orientation - the Lua way 
Metatables, Metamethods, classes and objects in Lua - (2014-03-18) 
Is Lua an Object Oriented language? - (2013-06-15) 
What is a metatable? How do I set one up? How do I use them? Lua - (2012-05-12) 
Using Lua tables as objects - (2012-05-11) 
Special __ methods you can use in Lua metatables - (2012-04-12) 
Weak references in Lua - what are they, and why use them? - (2012-04-04) 
Metaclasses (Python) and Metatables (Lua) - (2011-11-17) 
Tables as Objects in Lua - a gentle introduction to data driven programming - (2011-08-17) 
Private and Public - and things between - (2011-01-22) 
Lua Metatables - (2010-04-02) 
Is Lua an Object Oriented language? - (2010-04-01) 
Lua examples - coroutines, error handling, objects, etc - (2009-10-15) 
A fresh example - objects the Lua way - (2009-08-13) 
For Lua Programmers AND for Town Planners - (2009-08-02) 
Calling base class constructors - (2008-10-03) 
First class functions in Lua lead to powerful OO facilities - (2008-08-07) 
If you are learning Lua, here are some more examples - (2008-07-06) 
Towards Object Oriented Programming in Lua - (2008-06-30)
Some other Articles
A simple example - XML from a Ruby programHistory is all around usA more informed decision than ever beforeFor loop - checked once, or evety time? Ruby v Perl comparison and contrast__index and __newindex in Lua - metatable methodsOld trackways and routes near MelkshamThe bull on the footpathA walk on the Kennet and AvonError trapping in Lua - no exceptions.Hotel booking in Melksham made easy!