Indexing lua指数不';t返回多个值
对象应该为每个索引包含一对值Indexing lua指数不';t返回多个值,indexing,lua,Indexing,Lua,对象应该为每个索引包含一对值 object = {} function object:new() self.elements = {} setmetatable(self, self) return self end function object:append(first, second) self.elements[#self.elements + 1] = {first, second} end function object:__index(k) return
object = {}
function object:new()
self.elements = {}
setmetatable(self, self)
return self
end
function object:append(first, second)
self.elements[#self.elements + 1] = {first, second}
end
function object:__index(k)
return unpack(self.elements[k])
end
当我试图通过索引访问值时,我只得到该对的第一个值
o:append(1,2)
o:append(3,4)
> return o[1]
1
涉及产生预期结果的直接指数法
> return o:__index(1)
1 2
原因是什么?可能没有原因,但保留表索引的语义而不使用元表
该行为记录在中。当
\uu index
是一个函数时,解释索引元方法语义的代码会将返回的值调整为1。就是这样。非常遗憾。从_索引元方法多次返回不会破坏与以前编写的代码的向后兼容性。嗯,{t[i]}
应该具有长度1
。当索引操作返回多个值时,这种情况不再有效。