如何从一个函数中返回多个级别?

Lua中如何返回多个调用者级别?

就像Forth中在返回堆栈中删除一个值一样?

    arrSimpleConv=
          (function(result,base)if not base then return  nil end
  for rec in (function(rst) if not rst then return pairs({}) end
                       rst:Sort{{field=5,descent=true},{field=7},{field=10}}
                return rst.Records end)(base.RecordSet) do
    result[#result+1]= {
                         Type = rec:GetValue(5),
                         LegName = rec:GetValue(10),
                         PickName = rec:GetValue(11),
                         FlagHierarch = tonumber(rec:GetValue(30)),
                         Rules = (function(result,input)
                                for i,p in ipairs(input) do
                                  result[i] = table.unserialize(p)
                                end return result end)({},rec:GetValue(20, 0))
                       }
  end return result end)(arrSimpleConv or {},CroApp.GetBank():GetVocabulary():GetBase("XX"))
点赞
用户1424244
用户1424244

Lua中的一个函数(以及大多数编程语言)是一个单独的执行单元,除返回值外不会影响调用者。使用尾调用是直接跳转到另一个函数的唯一方法。

还可以使用错误机制跳出函数:

local signal = {}

function a()
  error(signal)
end

local status, err = pcall(a)
if err == signal then
  -- 从函数内部特定点“返回”
end

但我不建议这样做,因为它违反了函数作为独立实体的目的。

2021-01-23 14:38:54