【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.)

猜你喜欢

转载自blog.csdn.net/weixin_44559752/article/details/126171966