为什么 __add 不起作用?

所以我正在学习lua中的元表,所以我决定跟随一些教程。我正在尝试元表中的__add部分。但是由于某种原因,我一直收到错误消息(试图执行具有空值(nil)字段的算术运算)

aTable = {}
--为普通表分配值
for x = 1, 10 do
   aTable[x] = x
end

-- 元表
mt = {__add = function(table1, table2)
   sumTable = {}
   for i = 0, #table1 do
      sumTable[i] = table1[i] + table2[i]
   end
   return sumTable
end}

setmetatable(aTable, mt)

newTable = {}

newTable = aTable + aTable

for x = 1, #newTable do
   print(newTable[x])
end

这时我很糊涂,请给予帮助。

点赞
用户5636775
用户5636775

在 __add 函数中应该是:

for i = 1, #table1 do

因为你没有初始化 table[0],而是从下标 1 开始(这对于 Lua 伪数组来说确实是推荐的,许多操作都依赖于它)。

2015-12-06 19:54:53
用户1737
用户1737

@Ctx 是正确的,问题在于数组初始化和添加函数中的索引不同。但是解决问题的最佳方法是修改您的 __add 函数以处理传递的数组中的“空洞”,通过检查其中的 nil 条目来解决。

  for i = 0, #table1 do
    if (table1[i] and table2[i]) then
      sumTable[i] = table1[i] + table2[i]
    end
  end
2015-12-06 20:08:24
用户805875
用户805875

另一个缺失的部分:您没有在结果中设置相同的元表,这意味着像 aTable+aTableaTable+aTable+aTable 等等的操作都可以工作,但是 aTable+aTable+(aTable+aTable) 将失败。

修正并清理版本:

-- 元表
mt = {
    __add = function( table1, table2 )
        sumTable = {}
        for i = 1, #table1 do
            sumTable[i] = table1[i] + table2[i]
        end
        return setmetatable( sumTable, mt )
    end,
}

aTable = setmetatable( {}, mt )
-- 为普通表赋值
for x = 1, 10 do  aTable[x] = x  end

newTable = aTable + aTable

for x = 1, #newTable do  print( newTable[x] )  end

-- 另一个会失败的测试:
yetAnotherTable = newTable + newTable
for x = 1, #yetAnotherTable do  print( yetAnotherTable[x] )  end
2015-12-06 23:40:33