lua-users home
lua-l archive

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


Note that technicall the statement:
  a, b = nil, nil
is not a fetch of the variables, but assignment of different values (their old values are not accessed at all so they are already dead in the scope just before the assignment is performed. But there's currently no other standard way to delete a variable content.

May be we need a statement like:
  delete a, b
which is to be considered a "fetch" access to the values currently stored in the given variables, even if these values are actually not read
The accesses is in fact an explicit call to "close" these variables, as if we used:
  a[['_toclose']]()
  b[['_toclose']]()
Note: the double "[[]]" would be a very convenient syntax to access members of the metatable. It could also be written as:
  a.._toclose()
  b.._toclose()
when the keys in metatables are strings, usable as valid identifiers.


Le lun. 3 juin 2019 à 01:27, Philippe Verdy <verdy_p@wanadoo.fr> a écrit :
Le dim. 2 juin 2019 à 17:15, Andrew Gierth <andrew@tao11.riddles.org.uk> a écrit :
But even in the absence of finalizers, holding a (strong) reference to
an object has a visible effect: it prevents the object from vanishing
from a weak table due to collection. So the actual lifetime of a local
variable matters, even if its value is never fetched.

Yes but you don't define what is the "lifetime". For me a variable that is never fetched is already dead, it has NO value to keep it in "life" longer than needed.

If you want to keep if "live" because of underlying effects (e.g. to control more precisely when it must be killed), then you need an explicit statement later in the function to kill it; if you don't have such statement (making a reference to that variable later in the function, so that it cannot be killed before as it is explicitly alive before that explicit statement), then the compiler has the full right to decide to kill it as soon as this is valid (and the compiler should make all possible attempts, with a best effort approach, to do that as early as possible).

And you don't need a special declaration of that variable, what you need is an explicit reference to it.

So in:

  function()
     local a = {1}, b = somefunction()
     dosomething()
  end

the compiler can determine that the variables a,b are never fetched, so they is dead immediately and that declaration can be dropped even before calling dosomething(), and in this case it will not even compile the array initializer. And this code is then equivalent to:

  function()
     somefunction()
     dosomething()
  end

This is not the case with:

  function()
     local a = {1}, b = somefunction()
     dosomething()
     a = nil
     b = nil
  end

where the values assigned to a and b are kept alive during the call to domething(). No special keyword needed !