如何在lua 5.2中检索变量参数 [英] how to retrieve variable arguments in lua 5.2
问题描述
参考页都说类似
function this(...)
end
但是,当我尝试应用假定的arg
变量时,我得到的只是一个零引用.我为捕获参数所做的任何尝试都会在nil表中产生结果.我尝试强制使用local tab = {...}
,但仍然获得nil参考.我设法捕获参数的最接近的是select("#",...)
,它仅返回参数的数量.每当我尝试捕获此外部参数声明时,我都只会遇到另一个错误...
However when I try to apply the supposed arg
variable all I get is a nil reference. Any attempt I've made to capture the arguments results in a nil table. I've tried forcing a local tab = {...}
and still get the nil reference. The closest I've managed to get to capturing the arguments is a select("#",...)
which only returns the number of arguments. Whenever I try to capture this outside parameter declaration I get nothing but another error...
我一直在无济于事地调查着……我能在不强制通过表的情况下完成此工作吗?
I've been thoroughly looking into this with no avail... any way I can accomplish this without forcibly passing a table?
推荐答案
arg
参数仅适用于Lua 5.0.从Lua 5.1开始,改为使用vararg表达式...
.
The arg
argument is for Lua 5.0 only. Since Lua 5.1, the vararg expression ...
is used instead.
尝试一下:
function foo(...)
for k, v in ipairs{...} do
print(k, v)
end
end
foo('hello', 'world')
这篇关于如何在lua 5.2中检索变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!