A metatable of a metatable doesn't work in Lua

26 views Asked by At

In the following example:

local function callee()
    return 4
end

local function search(_, key)
    print("Here")
    if key == "__call" then
        return callee
    end
end

local object = {}
local mt1 = {}
local mt2 = { __index = search }
setmetatable(object, mt1)
setmetatable(mt1, mt2)
print(object())

it should be printed:

Here
4

But it is printed:

lua: metaOfMeta.lua:17: attempt to call a table value (local 'object')
stack traceback:
    metaOfMeta.lua:17: in main chunk
    [C]: in ?

Do you know why it is that? Are the reads from a metatable raw? I need this functionality for my class system.

0

There are 0 answers