lua-users home
lua-l archive

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




On Sat, Jun 29, 2019 at 1:09 AM Sergey Kovalev <kovserg33@gmail.com> wrote:
> I think that <toclose> is more powerful since it let you to define an
> object in a scope, but close it in another one.

do -- scope1
  local <toclose> obj1 = my_complex_object(1)
  local obj2 = my_complex_object(2)
  local <toclose> obj3
  local <toclose> obj4
  local <toclose> obj5
  do -- scope2
    obj3 = my_complex_object(3)
    obj4 = my_complex_object(4)
    obj5 = my_complex_object(5)
    obj4 = nil
    obj5 = my_complex_object(55)
 end -- end of scope 2
end -- end of scope 1

And what should be the difference in the fates of obj1, obj2, obj3,
obj4 and obj5 ?


This isn't permitted. Every line in scope2 will throw an error for assigning to a constant variable.

However, what you CAN do is call obj1.close() somewhere. Assuming that your code is smart enough to not muck with it when it's already closed, then the second close when the <toclose> variable goes out of scope is a no-op.

/s/ Adam