gpt4 book ai didi

lua - Lua 中的多个返回值

转载 作者:行者123 更新时间:2023-12-04 14:23:29 24 4
gpt4 key购买 nike

之前遇到过这个问题。
对于多返回值函数

fn=function() return 'a','b' end

电话
print(fn())返回 a b
但电话
print(fn() or nil)仅返回 a
为什么? or既然第一次调用成功应该没关系吧?

最佳答案

引自 Programming in Lua §5.1 – Multiple Results

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 results from the function. 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.



在您的示例中,返回值 fn()用作表达式( or 运算符的左操作数),因此只保留第一个值。

关于lua - Lua 中的多个返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com