lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 25-Jan-07, at 5:21 PM, Jan Schütze wrote:

Correct.
But only for error and exception handling.

I don't need it to handle an failure or an exception, its normal case - so there should be a normal function to do this, without such a big (and good working!) workaround, which Romulo posted.

Surely if you call a function "die", you're implying that it's a response to a failure or exceptional condition :) In which case, using an error() based exception system seems entirely appropriate.

If you want to do a "compressed return" from a nested recursion, you should think seriously about coroutines, which make that convenient, as well as being more flexible because they give the option of accepting a result or continuing a search.

For example:

trythis = coroutine.yield

function find()
  for value in some_query(database) do
    find2(database, value)
  end
end

function find2(database, value)
  for value2 in some_other_query(database, value) do
    find3(database, value, value2)
  end
end

function find3(database, v1, v2)
  for result in yet_another_query(database, v1, v2) do
    trythis(result)
  end
end

-- main search

local function search()
  local finder = coroutine.wrap(find)
  local result
  repeat
    result = finder()
    if checks_out(result) then return result end
    until not result
  end
  error "Not found"
end





Just posted mainly to help improving lua with what is often needed in developing web. applications. But you are correct, depends on the point of view, and where(/how often) you need such functionality.