lua-users home
lua-l archive

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

Paul Chiusano wrote:

I would like to store objects in a set, where the set is
implemented by a table mapping the objects to either true
or nil, depending on whether they are present or not.  In
order for this to work, I need equal objects (according to
__eq) to hash to the same location in the table.

I can think of a couple ways to get around this.  One is to
make it so that equal objects actually are the same table.
If you do this, you won't even need an __eq metamethod.  An
example of this is given in Programming in Lua 17.1:


The other way is to make your own "hash" function that
translates equal objects to the same string and unequal
objects to different strings.  Then you use the strings
(instead of the objects) as keys and the objects (instead of
true) as values.  This approach is actually used in the Lua
book to implement the memoization needed by the previous