lua-users home
lua-l archive

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


Em qui, 30 de mai de 2019 às 12:48, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> escreveu:
>
> > If true, then why not consider to default (mark) *all* 'local' to be
> > collected as soon as possible (when out of scope), at the given
> > reversed order, by the garbage collector (call __gc)?
>
> function newfile (name)
>   local f = io.open(name)
>   return f
> end   -- oops; 'f' goes out of scope, file is closed.
>
> Again, what goes out of scope are variables, not values.
>
> -- Roberto

It's OK, I'm not arguing, only try to understand <toclose>.

Is <toclose> action '__toclose' triggered by an 'out of scope' event?
then it does not need necessarily 'to be closed' something, right?

--- #####################################################
-- aggregate.lua
-- BEGIN

local sum = setmetatable({0},{
  __close = function(t) t[1] = t[1] + 1 end,
  __tostring = function(t) return t[1] end
})

local s1 = function()
  local <toclose> s = sum
  s[1] = s[1] + 1
  return s[1]
end -- s out of scope

print(s1(),s1(),sum)
-- END

rodrigo@rodrigo-X510UAR:~$ lua aggregate.lua
1    3    4
rodrigo@rodrigo-X510UA
--- #######################################################

It is very interesting and useful in fact, but '<toclose>' seems a
confusing word here.

Moreover, <const> is easily grasped, but <toclose> is not clear at all
depending on the context, as above, it is difficult to understand a
programmer's mind :)

Could it be possible to change the word '<toclose>' to something more
related to the actual behavior?

Thanks again for the amazing work!

-- 
Rodrigo Azevedo Moreira da Silva