From 947a372f5860a76fcafb4a2845abc322e440d6fc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Oct 2018 14:54:45 -0300 Subject: State in generic 'for' acts as a to-be-closed variable The implicit variable 'state' in a generic 'for' is marked as a to-be-closed variable, so that the state will be closed as soon as the loop ends, no matter how. Taking advantage of this new facility, the call 'io.lines(filename)' now returns the open file as a second result. Therefore, an iteraction like 'for l in io.lines(name)...' will close the file even when the loop ends with a break or an error. --- testes/files.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'testes/files.lua') diff --git a/testes/files.lua b/testes/files.lua index 9aae5913..a11c5e61 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -462,13 +462,13 @@ X - y; ]]:close() _G.X = 1 -assert(not load(io.lines(file))) +assert(not load((io.lines(file)))) collectgarbage() -- to close file in previous iteration -load(io.lines(file, "L"))() +load((io.lines(file, "L")))() assert(_G.X == 2) -load(io.lines(file, 1))() +load((io.lines(file, 1)))() assert(_G.X == 4) -load(io.lines(file, 3))() +load((io.lines(file, 3)))() assert(_G.X == 8) print('+') -- cgit v1.2.3-55-g6feb