数组作为参数列表LUA [英] array as parameter list lua

查看:194
本文介绍了数组作为参数列表LUA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我正在与一些LUA,与oldesh我的语言,并没有找到您能得到的参数为阵,像洙:

Today i was working some with lua,with that "oldesh" for me language,and did find what you can get arguments as array,like soo:

function foo(someting,...)
    local arrayofargs = arg
    -- code here
end

和现在,我intresting.Can认为是其他办法?你可以传递数组,而不是数组,但像参数列表,像这样:

And now,i'm intresting.Can that be in "other way"? Can you pass array,not as array,but like param list,Like so:

function bar(a1,a2)
    print(a1+a1)
end

function foo(someting,...)
    local arrayofargs = arg
    bar(arg)
end

是的,你不能做that.But我可以somehome成才做出这样的?

Yes,you cant do that.But can i somehome make someting like that?

推荐答案

如果你在谈论一个事实,即Lua中的旧版本给了一个自动 ARG 包含所有ARGS由可变参数函数接受,那么你可以做本地ARG = {...} 就在函数的开始。

If you're talking about the fact that old versions of Lua gave an automatic arg containing all args received by a vararg function, then you can just do local arg={...} right at the beginning of the function.

如果你想一个数组转换成列表,使用 table.unpack

If you want to convert an array into a list, use table.unpack.

所以,你的例子是

function foo(someting,...)
    local arg={...}
    bar(table.unpack(arg))
end

但是这并没有太大的意义,因为你可以做到这一点,这是更清晰,更简单的:

but this does not make much sense, since you can just do this, which is clearer and simpler:

function foo(someting,...)
    bar(...)
end

这篇关于数组作为参数列表LUA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆