lua-users home
lua-l archive

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

If I had understood correctly the question, the problem you have is that Lua allow nesting metatables when the __index metamethods are tables. In your case, because the first metatable has a __index function, you cannot chain a second one by returning "nil" in your function. I think you need something like this to make work your approach (written in Lua instead of C, but you can translate it):

-- a table with functions (which would be your list
-- of functions at your second __index metamethod)
local index_table = { foo = function(self,...) do something end }

-- the metatable for your objects
local mt = {
  __index = function(t, k)
      if type(k) == "number" then return some value end
      return rawget(t,k) or index_table[k]

local new_obj = setmetatable({}, mt)

On 26 December 2015 at 11:20, Marc Balmer <> wrote:
I have C API question:

In a first metatable I set the __index field to a function that checks if the key is a number.  If it is, it returns some data value, nil otherwise.  Now I want to nest a second metatable, that has a table of functions at the __index field.

I don't seem to get that to work. The functions in the second metatable are never called. I am probably setting the metatable on the wrong field of the first metatable.  So on which field of the first metatable do I set the second metatable, on the metatable itseld, the __index field or the (C) function returning either a value or nil?

Do some C examples of such nesting exist anywhere?