lua-users home
lua-l archive

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


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 !