lua-users home
lua-l archive

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

On Tue, May 01, 2007 at 01:29:13AM -0400, Luis Carvalho wrote:
> I don't know exactly what you mean, but you can certainly have types as
> userdata stored in a weak-valued table indexed by type name as the equivalence
> relation, say, and, to answer your question in the subject, objects as
> userdata that are different "table keys", but still equivalent if they are of
> the same type by the __eq metamethod. For example:

Thanks Luis, but I would like the opposite, if a == b, then t[a] == t[b]:

	a = uint64(1) 
	b = uint64(1) -- uint64() returns a userdata wrapping a uint64

	t = {}

	t[a] = 'this'
	t[b] = 'that'

	assert(a == b) -- this is possible using the __eq metamethod
	-- but this is less easy to arrange:
	assert(t[a] == t[b])

Since all userdata are distinct keys, as far as I can tell, lua has
user-defined "==" equivalence, but not user-defined "hash key"

So if I want to be able to have, for example, a numeric type where
objects are both equivalent table keys and == equivalent, I have to
implement a userdata factory using a weak-valued table.

Has there been any thought to a __hash() metamethod, so objects can
express key equivalence as well as == equivalence?