[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: [LUA]: Cannot use userdata as table index
- From: Coda Highland <chighland@...>
- Date: Fri, 10 Jul 2015 10:06:47 -0700
On Fri, Jul 10, 2015 at 9:45 AM, Tom N Harris <email@example.com> wrote:
> On Saturday, July 11, 2015 02:23:14 AM Борис Александров wrote:
>> http://prntscr.com/7r4ecw It seems to me that lua compares table index and
>> given value by address, not by _equal metamethod
> This is correct. Only the primitive
> Since the GetPlayer objects define a __tostring metamethod, you can convert the
> userdata to string for use as a table key.
> The __equals metamethod wouldn't be useful for tables. Lua would need a __hash
> metamethod that returns a value suitable as a table key. But I wonder what
> strange things could happen if A==B is not the same as hash(A)==hash(B)?
This is called a hash collision, and ANY hashtable implementation
(including the one already in Lua) has to deal with it. Essentially
what it means is that the entries of the hashtable are themselves
key-value lists, and you then do an equality check on the keys to find
out which of those values is the one you're actually looking for. The
idea is that the probability of a collision is low enough that you
won't need to check very many keys.