lua-users home
lua-l archive

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


>>>>> "Steve" == Steve Litt <slitt@troubleshooters.com> writes:

 Steve> I've been gone too long. What's a "weak table?"

A weak table is one whose keys and/or values are weak references to
objects - that is to say, they do not prevent the objects from being
garbage-collected (in the event of which the key-value pair is removed
from the table).

weakt = setmetatable({}, {__mode="v"})  -- table with weak values
t = {"foo"}
weakt[1] = t   -- does not prevent t from being GC'd
t = nil        -- no strong references to the {"foo"} table remain
collectgarbage()
collectgarbage()
for k,v in pairs(weakt) do print(k,v) end  -- prints nothing

 >> "grayagain" list; unless I'm misreading something, it looks like it's
 >> intended that g->weak and g->ephemeron should be empty outside of the
 >> atomic phase.

 Steve> Is ephemeron some kind of reserved word or part of the language?

an "ephemeron table" is a table with weak keys but strong values, that
is to say its metatable has __mode="k" - it prevents the value in each
key/value pair from being GC'd _as long as_ the key is reachable from
somewhere other than the value (or is not an object). Once the key is
unreachable, the entry is removed.

See http://www.lua.org/manual/5.3/manual.html#2.5.2

-- 
Andrew.