lua-users home
lua-l archive

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

@Sven, @JayCarlon, @Luiz - thanks for taking time to answer to my question. I think I understood the most of it. I am trying to stitch together the 3 answers, to understand it completely and finding some contradiction. Hopefully a follow-up answer might clear it up completely.

My question was indeed based on my observations around the upvalue semantics. I was following a tutorial to understand a particular way in which metatables are used, and the ability to return a local variable caused some confusion.

One contradiction (assumed) from @Sven and @Luiz's response regarding 'reference counting'. So - for return value (the object being referred-to), if it is not ref-counted, then I am guessing that semantics are always 'copy' ? That is, a copy of a local object is created and it is passed (sans referencing variable) as return value, and the function's local variable (and the object it was referring) is made GC candidate immediately. Is that correct ?

@Sven, regarding the upvalue catching semantics, where you say that it is done "conservatively", could you elaborate a bit ? I see three local variables (all tables), and only one of them really needs to have a lifetime, longer than that the function invocation, since it is returned. Was wondering if you call it conservative, because all 3 variables are not retained ?
@JayCarlson, the point about the edge-cases is understood. I saw some similar "extra specification behaviour" led and defined by the official (or main) Python implementation (written in C). Perhaps, this is why in spite of searching for an explanation of the semantics, in the Lua book, I didn't find many clues.

On Sun, Sep 22, 2013 at 1:51 AM, Luiz Henrique de Figueiredo <> wrote:
> So going out of scope, pops out the variable referring to the object
> on heap, and if object has no other reference, then it is queued for
> GC. Is that the right understanding ?

Not quite. Objects are only found have no referene in a GC cycle, not
when variables go out of scope. Lua does not use reference counting.