[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Basic question on weak keys
- From: Rena <hyperhacker@...>
- Date: Wed, 13 Jun 2012 16:15:18 -0600
On Wed, Jun 13, 2012 at 4:02 PM, Peter Cawley <firstname.lastname@example.org> wrote:
> On Wed, Jun 13, 2012 at 10:59 PM, Andrew Starks <email@example.com> wrote:
>> b = nil
>> --> nil
> As `b` is now nil, `a[b]` means `a[nil]`, and as `nil` is never a
> valid table key, `a[nil]` will give `nil` for all tables `a`.
Your check is broken: you set b=nil, so now you're looking up a[nil],
not a[original value of b].
Using an object to index a table doesn't keep any reference to it.
Only storing it in the table, as a key. Weak tables tell the garbage
collector to ignore the keys/values of this table when checking for
references. So, if you didn't have a reference to that key/value
anywhere else, it will eventually be collected and the pair removed
from the table. (If the key is removed, the value is also removed, and
vice-versa, since a key without a value is useless, and a value
without a key impossible.)
Sent from my Game Boy.