加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Chain Lua metatables

发布时间:2020-12-14 21:44:48 所属栏目:大数据 来源:网络整理
导读:我有一种情况,其中两个库L,M试图为_G设置metatable(分别命名为mL,mM).元数据中唯一的东西是__index. 如何链接这两个metatable,以便如果一个中的__index失败,则调用另一个中的索引? 解决方法 有一个metatable存储mL和mM,如果一个返回nil,检查另一个: local
我有一种情况,其中两个库L,M试图为_G设置metatable(分别命名为mL,mM).元数据中唯一的东西是__index.

如何链接这两个metatable,以便如果一个中的__index失败,则调用另一个中的索引?

解决方法

有一个metatable存储mL和mM,如果一个返回nil,检查另一个:

local metatbl = {}
metatbl.tbls = {mL,mM};
function metatbl.__index(intbl,key)
  for i,mtbl in ipairs(metatbl.tbls) do
    local mmethod = mtbl.__index
    if(type(mmethod) == "function") then
      local ret = mmethod(table,key)
      if ret then return ret end
    else
     if mmethod[key] then return mmethod[key] end
    end
  return nil
  end
end

setmetatable(_G,metatbl)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读