lua-users home
lua-l archive

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


Lua 5.0

Why can't you hook into the freeing of a regular old table via the
metatable?

I want to do this:

-------------------------------------------
z = {1,2}
mymeta = {__gc = function() print("x cleanup") end, __index = function()
return 4 end }
setmetatable(z,mymeta)
print(z[1])
print(z[8])
print(z[8])
z = nil
collectgarbage()
-------------------------------------------

and see it go


4
4
4
"x cleanup"


but instead it laughs at me as if I am a fool.  What it really prints
is:

1
4
4

So why the '1' - what about my __index function?  I guess that only
works if it can't find the thing in the table first -- which is ok for
some things but sucks for other uses I might try to put __index to.

And then of course, why no support for __gc invoking the function?  Or
am I somehow not preparing it for gc when I do the z = nil.  

Suggestions?  Helpful interpretations?  Can I hook into the freeing of a
table somehow with the metatable, and if not why the heck not? :)

Thanks!

-mdm