【lua】函数返回值的调整

今天maillist看到一个问题:函数返回值总是会被调整成最多一个值吗?

local a, b, c = f(), x -- f() is adjusted to 1 result (c gets nil)

先说结论:不是

lua会对函数的返回值个数进行调整,当且仅当函数不是最后一个右值的时候会将其调整为1。其它情况则调整成左值接收的数量。

简而言之,判断函数返回值会被调整成几个,看这个函数是不是最后一个右值即可
:若不是,则该函数的返回值被调整成1个;是最后一个右值,则左边有几个接收者,函数就有几个返回值(不足的部分用nil补齐)

举个栗子:

local function returnTest()
    return 3, 2, 1
end

-- 不是最后一个右值,返回值的个数调整成1
local a, b, c = "first", returnTest(), "last"
print("when function isn't the last element:\n", a, b, c)

-- 是最后一个右值,等号左边还有两个变量可以接收,返回值的个数调整成2
local a, b, c = "last", returnTest()
print("when function is the last element:\n", a, b, c)

-- 是最后一个右值,等号左边还有四个变量可以接收,返回值的个数调整成4。原函数只返回3个,不足的1个用nil补齐
local a, b, c, d, e = "last", returnTest()
print("when function is the last element:\n", a, b, c, d, e)

-- 是最后一个右值,由于接收者是函数,可以接收任意个值,所以返回值的个数不进行调整
print("when function is the last parameter of another function:\n", returnTest())

-- 是最后一个右值,由于无接收者,可以接收0个值,所以返回值的个数调整为0
returnTest()

输出:

when function isn't the last element:
        first   3       last
when function is the last element:
        last    3       2
when function is the last element:
        last    3       2       1       nil
when function is the last parameter of another function:
        3       2       1

5.4参考手册原文:

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.

Here are some examples:

 f()                -- adjusted to 0 results
 g(f(), x)          -- f() is adjusted to 1 result
 g(x, f())          -- g gets x plus all results from f()
 a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)
 a,b = ...          -- a gets the first vararg argument, b gets
                    -- the second (both a and b can get nil if there
                    -- is no corresponding vararg argument)
 
 a,b,c = x, f()     -- f() is adjusted to 2 results
 a,b,c = f()        -- f() is adjusted to 3 results
 return f()         -- returns all results from f()
 return ...         -- returns all received vararg arguments
 return x,y,f()     -- returns x, y, and all results from f()
 {f()}              -- creates a list with all results from f()
 {...}              -- creates a list with all vararg arguments
 {f(), nil}         -- f() is adjusted to 1 result

Any expression enclosed in parentheses always results in only one value. Thus, (f(x,y,z)) is always a single value, even if f returns several values. (The value of (f(x,y,z)) is the first value returned by f or nil if f does not return any values.)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值