lua(语法):调用一个 returns 超过 1 个值的函数,并将这些值用作参数,但没有额外的变量赋值行?
lua (Syntax): Calling a function that returns more than 1 value, and using those values as arguments, but without extra lines of variable assignment?
我遇到需要调用以下内容的情况:
function xy(i)
return i,i+8
end
并在另一个函数中使用它的输出。
function addition(x,y)
return x+y
end
有没有办法让它比这样写更优雅:
i.e. i=10; x,y=xy(10); addition(x,y)--28
我正在寻找类似的东西:
i.e. i=10; addition(xy(10)--where I somehow get two arguments here)
这两个函数都是在其他地方使用的泛型,合并是不可行的,可能编辑 what/how 他们 return 可能。
至少从 Lua 5.1 开始,以下工作 'as requested'。
When a function call is the last (or the only) argument to another call, all results from the first call go as arguments. [There are several examples using print.]
function xy(i)
return i,i+8
end
function addition(x,y)
return x+y
end
addition(xy(10)) -- 28
一种更冗长的方法,可能有助于在需要更多灵活性的类似情况下进行分解,将结果转换为 table,然后使用 unpack
(在 5.1 中添加).这种方法是 result -> table -> unpack -> arguments (per above).
addition(unpack({xy(10)})) -- 28
中的两种方法
我遇到需要调用以下内容的情况:
function xy(i)
return i,i+8
end
并在另一个函数中使用它的输出。
function addition(x,y)
return x+y
end
有没有办法让它比这样写更优雅:
i.e. i=10; x,y=xy(10); addition(x,y)--28
我正在寻找类似的东西:
i.e. i=10; addition(xy(10)--where I somehow get two arguments here)
这两个函数都是在其他地方使用的泛型,合并是不可行的,可能编辑 what/how 他们 return 可能。
至少从 Lua 5.1 开始,以下工作 'as requested'。
When a function call is the last (or the only) argument to another call, all results from the first call go as arguments. [There are several examples using print.]
function xy(i)
return i,i+8
end
function addition(x,y)
return x+y
end
addition(xy(10)) -- 28
一种更冗长的方法,可能有助于在需要更多灵活性的类似情况下进行分解,将结果转换为 table,然后使用 unpack
(在 5.1 中添加).这种方法是 result -> table -> unpack -> arguments (per above).
addition(unpack({xy(10)})) -- 28
中的两种方法