我是 Lua 的新手,所以可能错过了一些教程,但问题是:
我有原始表和元表以及我正在应用的几个运算符:
original = { 1, 2, 3 }
test = setmetatable(original, {
__add = function (lhs, rhs)
print('adds')
end,
__mul = function (lhs, rhs)
print('multiplies')
end
})
不幸的是,当我进行以下操作时:
test = test + 3
test = test * 3
我收到一个错误:
attempt to perform arithmetic on global 'test' (a table value)
没有找到关于这个问题的任何描述。我还注意到,如果 metatable 是一个单独的变量并传递给setmetatable
方法,那么它可以工作..