函数参数Lua 5.1的数量可变 [英] Variable number of function arguments Lua 5.1

查看:129
本文介绍了函数参数Lua 5.1的数量可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Lua脚本中,我试图创建一个具有可变数量参数的函数.据我所知,它应该像下面那样工作,但是以某种方式我在TI-NSpire上遇到了Lua 5.1的错误(全局arg为nil).我究竟做错了什么?谢谢!

In my Lua script I'm trying to create a function with a variable number of arguments. As far as I know it should work like below, but somehow I get an error with Lua 5.1 on the TI-NSpire (global arg is nil). What am I doing wrong? Thanks!

function equation:init(...)
    self.equation = arg[1]
    self.answers = {}
    self.pipe = {arg[1]}
    self.selected = 1

    -- Loop arguments to add answers.
    for i = 2, #arg do
        table.insert(self.answers, arg[i])
    end
end

instance = equation({"x^2+8=12", -4, 4})

推荐答案

Luis's answer is right, if terser than a beginner to the language might hope for. I'll try to elaborate on it a bit, hopefully without creating additional confusion.

您的问题是在特定于TI计算器的特定模型中嵌入Lua的情况下.因此,会有一些细节与独立的Lua不同,但是大多数这些细节将与您的环境中提供的库和函数有关.嵌入式版本的Lua与它的作者分发的独立Lua明显不同(尽管由于Lua是开放源代码,所以可能). ( Lua Binaries 是许多平台的二进制存储库.

Your question is in the context of Lua embedded in a specific model of TI calculator. So there will be details that differ from standalone Lua, but mostly those details will relate to what libraries and functions are made available in your environment. It is unusual (although since Lua is open source, possible) for embedded versions of Lua to differ significantly from the standalone Lua distributed by its authors. (The Lua Binaries is a repository of binaries for many platforms. Lua for Windows is a batteries-included complete distribution for Windows.)

您的示例代码具有一个混淆因素,即与计算器框架提供的类系统接口所需的细节.该详细信息主要表现为equation对象与被调用的equation:init()函数之间没有连接.既然有可以将其粘合起来的技术,那只会分散注意力.

Your sample code has a confounding factor the detail that it needs to interface with a class system provided by the calculator framework. That detail mostly appears as an absence of connection between your equation object and the equation:init() function being called. Since there are techniques that can glue that up, it is just a distraction.

据我所知,您的问题可以归结为如何在Lua中声明和实现可变参数函数(参数数量可变的函数).从对Luis答案的评论中,您正在阅读Lua编程在线版(又名PiL).您引用了第5.2节. PiL是该语言背景的一个很好的来源.不幸的是,可变参数函数是不断变化的特征之一.从Lua 5.0版开始,在线版本是正确的,但是TI计算器可能正在运行Lua 5.1.4.

Your question as I understand it boils down to a confusion about how variadic functions (functions with a variable number of arguments) are declared and implemented in Lua. From your comment on Luis's answer, you have been reading the online edition of Programming in Lua (aka PiL). You cited section 5.2. PiL is a good source for background on the language. Unfortunately, variadic functions are one of the features that has been in flux. The edition of the book on line is correct as of Lua version 5.0, but the TI calculator is probably running Lua 5.1.4.

在Lua 5中,用参数列表声明可变参数函数,参数列表以符号...结尾,该符号代表其余参数.在Lua 5.0中,调用是使用名为arg的魔术"局部变量实现的,该局部变量包含一个表,该表包含与...匹配的参数.这要求每个可变参数函数在被调用时都创建一个表,这是不必要的开销和垃圾收集器压力的来源.因此在Lua 5.1中更改了实现:...可以直接在调用的函数中用作匹配参数的别名,但实际上未创建任何表.相反,如果需要对参数进行计数,则编写select("#",...),如果需要第n个参数的值,则编写select(n,...).

In Lua 5, a variadic function is declared with a parameter list that ends with the symbol ... which stands for the rest of the arguments. In Lua 5.0, the call was implemented with a "magic" local variable named arg which contained a table containing the arguments matching the .... This required that every variadic function create a table when called, which is a source of unnecessary overhead and pressure on the garbage collector. So in Lua 5.1, the implementation was changed: the ... can be used directly in the called function as an alias to the matching arguments, but no table is actually created. Instead, if the count of arguments is needed, you write select("#",...), and if the value of the nth argument is desired you write select(n,...).

您的示例中一个令人困惑的因素回到了类系统.您要声明函数equation:init(...).由于此声明使用冒号语法,因此等效于编写equation.init(self,...).因此,当最终通过类框架使用__call元方法进行调用时,实际的第一个参数名为self,并且零个或多个实际参数将与...匹配.

A confounding factor in your example comes back to the class system. You want to declare the function equation:init(...). Since this declaration uses the colon syntax, it is equivalent to writing equation.init(self,...). So, when called eventually via the class framework's use of the __call metamethod, the real first argument is named self and the zero or more actual arguments will match the ....

正如下面Amr的注释所指出的,表达式select(n,...)实际上返回第n个参数上的所有值,这在这种情况下对于构造self.answers尤其有用,但也可能导致初始化的错误. self.pipe.

As noted by Amr's comment below, the expression select(n,...) actually returns all the values from the nth argument on, which is particularly useful in this case for constructing self.answers, but also leads to a possible bug in the initialization of self.pipe.

这是我对equation:init()定义所要达到的目标的修订近似值,但请注意,我手头没有TI计算器之一,并且未经测试:

Here is my revised approximation of what you are trying to achieve in your definition of equation:init(), but do note that I don't have one of the TI calculators at hand and this is untested:

function equation:init(...)
    self.equation = select(1, ...)
    self.pipe = { (select(1,...)) }
    self.selected = 1
    self.answers = { select(2,...) }
end

在上面显示的修订版中,我写了{(select(1,...))}来创建一个表,该表只包含一个元素,它是第一个参数,而{select(2,...)}来创建一个表,其中包含所有其余参数.尽管可以通过这种方式插入到表中的值的数量受到限制,但是该限制与函数的返回值的数量或可以传递给函数的参数的数量有关,因此不能引用...超出的范围.请注意,通常情况可能并非如此,编写{ unpack(t) } 可能会导致未复制t的所有数组部分.

In the revised version shown above I have written {(select(1,...))} to create a table containing exactly one element which is the first argument, and {select(2,...)} to create a table containing all the remaining arguments. While there is a limit to the number of values that can be inserted into a table in that way, that limit is related to the number of return values of a function or the number of parameters that can be passed to a function and so cannot be exceeded by the reference to .... Note that this might not be the case in general, and writing { unpack(t) } can result in not copying all of the array part of t.

编写函数的效率稍低的方法是在传递的参数上编写一个循环,这是我最初的回答中的版本.如下所示:

A slightly less efficient way to write the function would be to write a loop over the passed arguments, which is the version in my original answer. That would look like the following:

function equation:init(...)
    self.equation = select(1, ...)
    self.pipe = {(select(1,...))}
    self.selected = 1

    -- Loop arguments to add answers.
    local t = {}
    for i = 2, select("#",...) do
        t[#t+1] = select(i,...)
    end
    self.answers = t
end

这篇关于函数参数Lua 5.1的数量可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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