[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: break for iterators
- From: Shmuel Zeigerman <shmuz@...>
- Date: Fri, 22 Feb 2008 18:41:54 +0200
Alexey Malyshev wrote:
This code example causes error after ~500 for loop iterations.
for k=1,1000 do
for line in io.lines() do
When iteration cycle breaks, file handles doesn't close.
Does break keyword wrong for iterator's loop?
It isn't wrong, but the "generic for" just terminates the loop on
breaks, so the file remains open until the garbage collector will close
it. The following example should work (untested):
> for k=1,1000 do
> for line in io.lines() do
Wouldn't it be possible to enhance the "generic for" by adding an extra
"break" call to the iterator (with a nil or false argument), to let it
do the cleanup when needed? That may break the existing applications
though (no pun intended).