lua-users home
lua-l archive

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


I'm a bit ashamed of this question, but my ideas are somewhat confused on this matter. Suppose I have this code:

---

t = {}

do

  local a = 5

  function t.dbl()
    a = a * 2
    print(a, _G.a)
  end

  function t.inc()
    a = a + 1
    print(a, _G.a)
  end

end

t.dbl() --> 10 nil
t.inc() --> 11 nil

---

So both functions share the same 'a', as expected, and this 'a' is not global (again, as expected).

The first question is: where is 'a' located? Do both functions share 'a' as an upvalue? If so, how can they hold a reference to a simple value (as opposed, for example, as to a table)?

The second question, which prompted me to ask, is: if I remove both functions:
  t = {}
does 'a' get collected at gc? (assuming there are no other reference to it)

  Enrico