lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In the following code:

> table = {}
> table.var = 3
> metatable = {}
> metatable.__index = function(table, key) print("__index called") end
> setmetatable(table, metatable)
> print(table.var)
3
> print(table.var2)
__index called
nil

Why doesn't the __index metamethod get called when I access table.var? The
reference manual simply says '"index": the indexing access table[key]' to
describe when this function gets called.

I am trying to protect a table from being modified, and I thought the best
way to do this would be through metatables, but this doesn't seem to
working, because my metamethods are not being called when I thought they
would be (specifically, the __index method is not being called). How do I
protect this table?

Thanks,
Tim
-- 
Tim Conkling
tconkling@brown.edu