Closed asmagill closed 3 years ago
Thinking about it a little more, I can understand it returning 3 items (the function, the object, and the argument for the first object (i.e. nil)), but doesn't that also apply from 5.2 forward?
But the additional copy of the directory object at the end still eludes me.
This is new to-be-closed
variable
So you are saying that for
now takes a fourth argument for it's iterators/generators which it treats with <close>
when used like this:
for file in lfs.dir("/home/me") do ... end
If so, that's what I missed in the docs.
Update, just found it in the docs:
The loop starts by evaluating explist to produce four values: an iterator function, a state, an initial value for the control variable, and a closing value.
Then, at each iteration, Lua calls the iterator function with two arguments: the state and the control variable. The results from this call are then assigned to the loop variables, following the rules of multiple assignments (see §3.3.3). If the control variable becomes nil, the loop terminates. Otherwise, the body is executed and the loop goes to the next iteration.
The closing value behaves like a to-be-closed variable (see §3.3.8), which can be used to release resources when the loop ends. Otherwise, it does not interfere with the loop.
Ok, that's good enough for me. Thanks!
@asmagill glad you were able to figure it out :)
I'm trying to understand the changes for Lua 5.4 and am curious about https://github.com/keplerproject/luafilesystem/blob/master/src/lfs.c#L728-L734 in
dir_iter_factory
, i.e. these lines:Is there a reason an extra
nil
and copy of the directory object are added to the returned values? Has there been a change for generators that I missed in the release notes or docs for Lua 5.4 (an entirely likely possibility as it has happened before)?Just curious if I need to be modifying generators in our own code as well (haven't had an issue yet, but better safe than sorry!)