Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Lua中的多个返回值函数中优雅地引用返回值?_Lua - Fatal编程技术网

如何在Lua中的多个返回值函数中优雅地引用返回值?

如何在Lua中的多个返回值函数中优雅地引用返回值?,lua,Lua,假设我有一个函数: function func() return 1, 2, 3 end 有没有一种方法可以优雅地引用各个返回值?比如 if func() == 1 then print("stuff") end 而是引用第二个或第三个返回值 我知道你能做到 if ({func()})[2] == 2 then ... 但这看起来很糟糕,还不如 _,v = func() if v == 2 then ... 我想做这样的事情 if func() == _,2 then ...

假设我有一个函数:

function func()
    return 1, 2, 3
end
有没有一种方法可以优雅地引用各个返回值?比如

if func() == 1 then
  print("stuff")
end
而是引用第二个或第三个返回值

我知道你能做到

if ({func()})[2] == 2 then ...
但这看起来很糟糕,还不如

_,v = func()
if v == 2 then ...
我想做这样的事情

if func() == _,2 then ...

这将是
选择

if select(2, func()) == 2 then ... end

print(select(1, func()) -- prints 1 2 3
print(select(2, func()) -- prints 2 3
print(select(3, func()) -- prints 3
print(select('#', func()) -- prints 3, the total number of arguments received