lua-users home
lua-l archive

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


I have two related questions. 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. The easiest thing would be if there
were a __hash metamethod, but there isn't one. (or is there? it isn't
mentioned in the documentation...) If numbers and strings are the only
types with consistent equals and hashing functions, it is pretty
difficult to use tables or other objects as keys in a table. I can't
really think of any general way of getting around this, aside from
implementing my own hashtable.

My other question is about using the equals metamethod with the
'private' style classes, (discussed in section 16.4 of the book).
According to ch 13, "Lua calls the equality metamethod only when the
two objects being compared share this metamethod." In the private
style of classes, objects of the same 'type' actually have different
metamethods, so the __eq metamethod never even gets called to compare
two objects. Am I basically forced to use the public style of classes
if I want to define an __eq metamethod? (I can maybe think of one
really ugly hack around this, but it's not robust)