Re: [proposal] close file on io.lines("filename", "*a")()
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: [proposal] close file on io.lines("filename", "*a")()
- From: "Soni \"They/Them\" L." <fakedme@...>
- Date: 2019年7月30日 14:44:04 -0300
On 2019年07月30日 2:12 p.m., Sean Conner wrote:
It was thus said that the Great Soni They/Them L. once stated:
> since lua doesn't have refcounting and some ppl wanna do basically
> io.open(file):read("*a") I wanna propose io.lines("filename", "*a")()
> (or alternatively io.lines("filename", "*a", "*l")) should close the
> file as if the iterator ran out.
Lua 5.4 already does that.
[spc]lucy:/tmp>lua-54
Lua 5.4.0 Copyright (C) 1994-2019 Lua.org, PUC-Rio
> print(io.lines("tabs"))
function: 0x8b39900 nil nil file (0x8b410f8)
That final value marks the descriptor for closing when the iterator is
done. If you want to automatically close the file after reading the
contents:
local data do
local <toclose> f = io.open(myfile)
data = f:read("a")
end
-- f is closed, and data has the contents.
-spc
no, sorry, I think you misunderstood.
io.lines("filename", "*a")()
note the call at the end. this reads the whole file. ideally it'd also
close it.
compare to:
io.open("filename"):read("*a")