Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

lua - How to make a multi-level return from a function?

how return more then 1 level of callers in lua?

something like in Forth when your drop 1 value from 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"))
question from:https://stackoverflow.com/questions/65855740/how-to-make-a-multi-level-return-from-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...