加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua – 我怎么能获得任何回报?

发布时间:2020-12-15 00:17:00 所属栏目:大数据 来源:网络整理
导读:我有兴趣抓取函数的任何返回格式.例如 function foo() return 1endlocal result = foo() -- foo is numeric 1function foo() return {1,2,3}endlocal result1,result2,result3 = foo()local result = foo() -- this is bad as result is `1` but `2` and `3`
我有兴趣抓取函数的任何返回格式.例如
function foo()
  return 1
end

local result = foo() -- foo is numeric 1

function foo()
  return {1,2,3}
end
local result1,result2,result3 = foo()
local result = foo() -- this is bad as result is `1` but `2` and `3` are lost

function foo()
  return 1,3
end
local result = foo() -- foo is a table with all the numbers,that's ok

我正在构建一个将使用代理函数覆盖函数的分析器,但我需要知道返回的数据,然后检查它的type()并相应地访问`但是从代码可以看出我无法访问所有3种情况一种方法.有没有 ?

解决方法

如果已知最大返回数,请使用类似的内容
v1,v2,v3 = foo()

但你无法判断foo是返回两个值还是三个,最后一个是nil.

强大的解决方案是收集表中的所有回报:

v = table.pack(foo())

然后v.n包含返回值的数量,包括所有nils.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读