lua-users home
lua-l archive

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


I'll say off the bat that i'm very much for this extension. A behavior complexity issue does, however, come to mind: what do we do with a method reference once the object is referred to nowhere else but in said method? As far as I can tell, there are three possible approaches:

simple: treat the method reference exactly as if it were a closure over the object.
object ownership: methods cease to exist once the object ceases to exist
weak upvalue: a method's reference to the object is weak (just as a weak tables's entries): once all references to the object disappear, it may be garbage collected. That is, the garbage collector disregards the method's object reference when doing its thing.

That is:


local foo = {
   bar = function(self, meh)
      return self.x .. meh
   end,
   x = "hey "
}
local foosmethod = foo:bar
print(foosmethod("dude"))
-- >> "hey dude"
foo = nil
print(foosmethod("dude"))
-- simple: >> "hey dude"
-- object ownership: >> nil
-- weak upvalue: >> error: can't index self (a nil value)




- Leo