加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua unpack()搞乱参数

发布时间:2020-12-15 00:26:41 所属栏目:大数据 来源:网络整理
导读:我有这个测试功能,它只是打印传递给它的值 function test1(...) for k,v in ipairs(arg) do print(v) endendfunction test2(...) for k,v in pairs(arg) do print(v) endend-- GOOD behaviortest1(1,2,3,4) -- produces 1 2 3 4test2(1,4) -- produces 1 2 3
我有这个测试功能,它只是打印传递给它的值
function test1(...)
  for k,v in ipairs(arg) do
    print(v)    
  end
end

function test2(...)
  for k,v in pairs(arg) do
    print(v)    
  end
end

-- GOOD behavior
test1(1,2,3,4) -- produces 1 2 3 4
test2(1,4) -- produces 1 2 3 4

-- BAD behavior
test1( unpack({1,2}),4) -- produces 1 3 4
test2( unpack({1,4) -- produces 1 3 4 3

有人可以向我解释这种行为吗?

解决方法

此行为不是特定于解压缩.
Lua Reference Manual说:

“Both function calls and vararg expressions can result in multiple values. If a function call is used as a statement (see §3.3.6),then its return list is adjusted to zero elements,thus discarding all returned values. If an expression is used as the last (or the only) element of a list of expressions,then no adjustment is made (unless the expression is enclosed in parentheses). In all other contexts,Lua adjusts the result list to one element,either discarding all values except the first one or adding a single nil if there are no values.

(我的重点)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读