[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: equals metamethod and hash functions
- From: PA <petite.abeille@...>
- Date: Thu, 9 Jun 2005 20:02:45 +0200
On Jun 08, 2005, at 00:27, Paul Chiusano wrote:
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...)
FWIW, LU [1] happily uses string representation of complex objects and
let Lua handle the hashing itself.
Assuming a Map [2] like structure to store "complex" objects [3]:
-- instance method to convert a given key to a string
this.stringForKey = function( aKey )
if ( aKey ~= nil ) and ( type( aKey ) ~= "string" ) then
if ( this.isKindOf( aKey ) == true ) then
aKey = aKey.toString()
else
aKey = tostring( aKey )
end
end
return aKey
end
-- instance method to return a value for a given key
this.get = function( aKey )
if ( aKey ~= nil ) then
aKey = this.stringForKey( aKey )
return this.content()[ aKey ]
end
return nil
end
-- instance method to associate a given value with a given key
this.put = function( aKey, aValue )
if ( aKey ~= nil ) then
aKey = this.stringForKey( aKey )
this.content()[ aKey ] = aValue
end
return this
end
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/
[1] http://dev.alt.textdrive.com/browser/lu/
[2] http://dev.alt.textdrive.com/file/lu/LUMap.lua
[3] http://dev.alt.textdrive.com/file/lu/LUObject.lua