-2

How can I return more than one level of callers in Lua?

Something like in Forth when you drop one value from the stack of returns?

    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"))
3
  • 2
    Please post an example.
    – Dai
    Commented Jan 23, 2021 at 4:31
  • As far as I know, Lua doesn't really support function composition the way that Forth does, so I don't think the stack is reified like that.
    – Dai
    Commented Jan 23, 2021 at 4:32
  • From the example you've posted it's still unclear what are the levels of callers which you want to return. Commented Jan 23, 2021 at 8:06

1 Answer 1

2

A function in Lua (and most programming languages) is a single unit of execution, and does not affect the caller except for the returned values. Using a tail call is the only way of directly jumping to another function.

It is also possible to use the error mechanism to jump outside a function:

local signal = {}

function a()
  error(signal)
end

local status, err = pcall(a)
if err == signal then
  --"returned" from the specific point inside a function
end

I would not recommend this however, since it defeats the purpose of functions as independent entities.

Not the answer you're looking for? Browse other questions tagged or ask your own question.