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"
end
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"],
"(%a+):.(.-)\n") do
if k == what then
rv = rv .. v .. "\n"
end
end
return rv
end
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:")
io.write(picture.name)
print ("\nPainters of pictures:")
io.write(picture.painter)
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:
The Haywain
Whistler's Mother
Painters of pictures:
Constable
Whistler
[trainee@holt lm10]$
(written 2010-04-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U107 - Object Orientation - the Lua way [1692] Towards Object Oriented Programming in Lua - (2008-06-30)
[1699] If you are learning Lua, here are some more examples - (2008-07-06)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1819] Calling base class constructors - (2008-10-03)
[2318] For Lua Programmers AND for Town Planners - (2009-08-02)
[2359] A fresh example - objects the Lua way - (2009-08-13)
[2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
[2701] Is Lua an Object Oriented language? - (2010-04-01)
[2703] Lua Metatables - (2010-04-02)
[3142] Private and Public - and things between - (2011-01-22)
[3396] Tables as Objects in Lua - a gentle introduction to data driven programming - (2011-08-17)
[3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17)
[3683] Weak references in Lua - what are they, and why use them? - (2012-04-04)
[3694] Special __ methods you can use in Lua metatables - (2012-04-12)
[3727] Using Lua tables as objects - (2012-05-11)
[3730] What is a metatable? How do I set one up? How do I use them? Lua - (2012-05-12)
[4117] Is Lua an Object Oriented language? - (2013-06-15)
[4248] Metatables, Metamethods, classes and objects in Lua - (2014-03-18)
[4273] Dot or Colon separator between table name and member in Lua - what is the difference? - (2014-05-06)
[4572] Tables with values and code in Lua - looks like an object? - (2015-11-05)
[4573] Classic style OO code - in Lua - (2015-11-05)
[4753] Lua, Tcl, Python, C and C++ courses - at our Melksham HQ or on your site - forward from July 2017 - (2017-07-02)
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!