Pergunta

I am implementing a lua iterator and I wonder what the iterator factory (the function that creates the closure which is used to iterate over the iteratable, see list_iter on http://www.lua.org/pil/7.1.html ) should return in case of nothing to iterate.

E.g. lets say I would implement a list type and my list object just would not have any entries at all. So I would suppose that the body ... of for i in myiterator do ... end is just never visited and the script execution continues as if nothing happened.

At the moment I return nil and Lua(jit) complains about attempt to call a nil value. The same happens if I do not return a return value.

Foi útil?

Solução

You can return a closure that returns nil like this:

function nil_iter()
  return function() return nil end
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top