lua-users home
lua-l archive

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


On Wed, Jun 13, 2012 at 4:02 PM, Peter Cawley <lua@corsix.org> wrote:
> On Wed, Jun 13, 2012 at 10:59 PM, Andrew Starks <andrew.starks@trms.com> wrote:
>> b = nil
>> print(a[b])
>> --> 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.