Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should a lua iterator factory return in case of nothing to iterate

Tags:

lua

luajit

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.

like image 408
wirrbel Avatar asked Oct 04 '22 01:10

wirrbel


1 Answers

You can return a closure that returns nil like this:

function nil_iter()
  return function() return nil end
end
like image 171
Yu Hao Avatar answered Oct 07 '22 22:10

Yu Hao