Getting arg to work in a varag function in Lua 5.2 (integrated in Delphi)

醉酒当歌 提交于 2020-01-14 13:25:50

问题


When using the Lua 5.2 API, the code below prints "nil"

function __debug(szName, ...)
    print(type(arg));
end

__debug("s", 1, 2, 3, 4);

But this code does work when using Lua 5.1, and prints "table"


回答1:


If you are referring to vararg function, the arg table was deprecated already in Lua 5.1. In Lua 5.2, you can use table.pack to create arg if you need it:

function debug(name, ...)
    local arg = table.pack(...)
    print(name)
    for i=1,arg.n do
        print(i, arg[i])
    end
end



回答2:


That's because arg has been deprecated since Lua 5.1. It only remained as a compatibility feature.

References: Lua 5.1 manual, unofficial LuaFaq

a workaround is using this line to generate a table called arg:

local arg={...}


来源:https://stackoverflow.com/questions/9786051/getting-arg-to-work-in-a-varag-function-in-lua-5-2-integrated-in-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!