lua-users home
lua-l archive

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


Hi guys,

Has anybody given some thought to the usefulness of an __iter
metamethod? It would specifically be called by a for-statement if it
was not given an explicit iterator function. For example,
t = {10,20,30}
setmetatable(t,{
   __iter = ipairs
}
for i,v in t do
  print(i,v)
end

Which is precisely the old Lua 5.0 behaviour ;)

But this can be generalized; file objects could have an __iter
metamethod, and then one can iterate over all input lines in a
Pythonesque way:

for line in io.stdin do
...
end

The motivation is to make the concept of _sequence_ more useful in Lua...

steve d.