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

StackOverflow https://stackoverflow.com/questions/18574888

  •  27-06-2022
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

You can return a closure that returns nil like this:

function nil_iter()
  return function() return nil end
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top