diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-10-31 14:54:45 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-10-31 14:54:45 -0300 |
commit | 947a372f5860a76fcafb4a2845abc322e440d6fc (patch) | |
tree | e35840847207c850af5262f93f863f583d2af76d /testes/files.lua | |
parent | e073cbc2e538369e0611abfc9948f301aea6aef3 (diff) | |
download | lua-947a372f5860a76fcafb4a2845abc322e440d6fc.tar.gz lua-947a372f5860a76fcafb4a2845abc322e440d6fc.tar.bz2 lua-947a372f5860a76fcafb4a2845abc322e440d6fc.zip |
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.
Diffstat (limited to 'testes/files.lua')
-rw-r--r-- | testes/files.lua | 8 |
1 files changed, 4 insertions, 4 deletions
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 | |||
462 | - y; | 462 | - y; |
463 | ]]:close() | 463 | ]]:close() |
464 | _G.X = 1 | 464 | _G.X = 1 |
465 | assert(not load(io.lines(file))) | 465 | assert(not load((io.lines(file)))) |
466 | collectgarbage() -- to close file in previous iteration | 466 | collectgarbage() -- to close file in previous iteration |
467 | load(io.lines(file, "L"))() | 467 | load((io.lines(file, "L")))() |
468 | assert(_G.X == 2) | 468 | assert(_G.X == 2) |
469 | load(io.lines(file, 1))() | 469 | load((io.lines(file, 1)))() |
470 | assert(_G.X == 4) | 470 | assert(_G.X == 4) |
471 | load(io.lines(file, 3))() | 471 | load((io.lines(file, 3)))() |
472 | assert(_G.X == 8) | 472 | assert(_G.X == 8) |
473 | 473 | ||
474 | print('+') | 474 | print('+') |