lua-users home
lua-l archive

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


On Sat, Jun 22, 2019 at 2:33 PM Andrew Gierth
<andrew@tao11.riddles.org.uk> wrote:
>
> >>>>> "Patrick" == Patrick Donnelly <batrick@batbytes.com> writes:
>
>  >> In 5.4, io.lines explicitly _does_ integrate with the generic for
>  >> loop to ensure the file is closed (if it was opened and isn't just
>  >> using io.input). What makes you think there is not?
>
>  Patrick> Sorry, I meant that there is no integration with generic for
>  Patrick> loop and the toclose variables, i.e. that breaking out of the
>  Patrick> for loop will cause the state value to be closed.
>
> But this integration does exist. Why do you think it does not?
>
> The generic for loop in 5.4 takes 4 values, not 3, and the 4th one is
> treated as being assigned to a toclose variable. That is to say,
>
>   for foo in a,b,c,d do ... done
>
> will invoke d's __close metamethod when the loop is exited by any means.
>
> io.lines("file") now returns 4 values: readline_func, nil, nil, file
> where the readline_func is a closure that returns the next line from the
> file, and "file" is the filehandle that was opened, which will be closed
> via its __close metamethod.

Oh, I missed that in the change notes. Sorry for the noise and thanks
for pointing that out!

-- 
Patrick Donnelly