lua-users home
lua-l archive

Re: Operators on functions

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


On Fri, Nov 22, 2013 at 6:38 AM, H. Conrad Cunningham
<hcc@cs.olemiss.edu> wrote:
> On Tue, Nov 19, 2013 at 10:26 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
> Also thanks. Weak tables were not in my "working set" of Lua programming
> features and techniques at the time (or yet for that matter).
Here's the gist for weak keys:
 is_cell = setmetatable({}, {__mode="k"}) -- a table with weak keys
 function cons(a,b)
 local cell = {a,b} -- cell == table: 0x12345678
 is_cell[cell] = true
 return cell
 end
 c = cons(1,2)
 is_cell[c] --> true
 for k,v in pairs(is_cell) do print(k,v) end --> table: 0x12345678, true
 c = nil
 collectgarbage()
 for k in pairs(is_cell) do print(k) end --> nothing
The presence of the key in the weak table does not prevent it from
being garbage collected. There's a chapter in PiL that covers it in
more in depth.
-- Pierre-Yves

AltStyle によって変換されたページ (->オリジナル) /