Main Content

__index and __newindex in Lua - metatable methods

Archive - Originally posted on "The Horse's Mouth" - 2010-04-05 11:32:01 - Graham Ellis

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]$