1

我正在尝试从其他应用程序的脚本中模拟某些功能的执行。该应用程序具有包含函数的 Lua 库list(),它返回表,其中键是字符串 UUID,值只是字符串,例如local tbl = { "0000..-0000-..." = "someString", etc... }. 该表可以在 for 循环中迭代,例如

local lib = require("someLibrary.lua");

for key, value in lib.list() do
    -- do something with keys and values, like print
end

-- or it can be used like this

local tbl = lib.list();
for key, value in tbl do -- tbl works as pairs(tbl) and works exactly how code on top
    -- do something with keys and values, like print
end

那么问题来了,我如何实现 __call 元方法以作为 pair() 或 next() 等工作?

谢谢

4

2 回答 2

0

是的!我设法找到了我的问题的答案。我找到了我试图复制的库的源代码。它实际上使用局部变量来遍历表

这是我制作lib.list()lst工作的代码

function lib.list(filter, exact)
    if filter == nil then filter = '' end;
    if exact == nil then exact = false end;

    local list = Component.list(filter, exact); -- just gets table from app that written in C#, treat like someClass.getSomeTable()

    local list_mt = {}; -- metatable that assigns to above "list" table
    local key = nil; -- key that will be used in iterator

    function list_mt.__call() -- __call metamethod for "list" table
        key = next(list, key);
        if key then
            return key, list[key]
        end
    end

    return setmetatable(list, list_mt); -- return table with assigned metatable that contains __call metamethod
end
于 2020-06-13T05:42:37.740 回答
0

pairs(tbl)返回next,tbl,nil

也一样

setmetatable(tbl,{__call=function (t) return next,t,nil end})

然后你可以写

for key, value in tbl() do
        print(key, value)
end

我不认为你可以避免()那里。

于 2020-06-12T16:25:05.263 回答