且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

table.unpack()仅返回第一个元素

更新时间:2023-02-19 10:56:14

table.unpack返回多个值.在这种情况下,定义的行为是,如果它不是表达式列表中的最后一个,则除第一个返回值之外的所有值都将被丢弃.

table.unpack returns multiple values. The defined behavior in that case is that if it is not the last one in a list of expressions then all but the first returned value will be discarded.

摘录自:

Lua始终根据调用的情况调整函数的结果数量.当我们将函数作为语句调用时,Lua会丢弃所有结果.当我们使用调用作为表达式时,Lua仅保留第一个结果.仅当调用是表达式列表中的最后一个(或唯一)表达式时,我们才能获得所有结果.

Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all of its results. When we use a call as an expression, Lua keeps only the first result. We get all results only when the call is the last (or the only) expression in a list of expressions.

作为一种解决方法,您可以将其余参数附加到表中,并以这种方式使表成为最后一个参数.

As a workaround you can append the rest of the arguments to the table and make the table the last argument that way.