lua-users home
lua-l archive

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

Isn't the "obvious" way to empty a table to...

   key = next(table, nil)
   if (key) then -- OK, lookup type checking to 
                 -- ensure nil rather than false
      table[key] = nil
until (not key) -- See previous comment

-----Original Message-----
[]On Behalf Of
Sent: Wednesday, November 05, 2003 9:38 AM
To: Lua list
Subject: RE: deleting tables from C

Hi, Nick

I found this reply a little confusing:

> If the table is a global, it is an entry in the global table, 
> i.e. _G[<table_name>] = nil  (You can use the pseudo-indices here or the
> legacy lua_setglobal macro)

> If the table is a local, you lua_setlocal()

The table isn't either a global or a local; it is an object in the heap.
Globals and/or locals can refer to the object.
When there are no references left to the object, the garbage collector
will deal with it.

So you "delete" a table or function by making sure that you do not
unnecessarily retain any references to it, and then it will go away
on its own accord.

A different question is "how do you empty a table?". This has come
up a few times on the list, and there doesn't seem to be a 
satisfactory answer, although the need rarely arises and I did
at one point post a metamethod-based solution for the cases
when it does.