lua-users home
lua-l archive

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


Assuming one has some forms of object structure:

http://article.gmane.org/gmane.comp.lang.lua.general/13478

How would such an object handle equality in general? Specially in Lua's "native" objects, ie "tables".

For example, assuming an "URL" object subclassing the root "LUObject":

function LUObject()
     local self = {}

     local isEqual = function( anObject )
            return self == anObject
     end

     return
     {
            isEqual = isEqual
     }, self
end

function URL( aValue )
     local self, super = LUObject();

	self.value = aValue

	local value = function
	    return self.value
	end

	local isEqual = function( anObject )
	    if super.isEqual( anObject ) == false then
		    if value == anObject.value() then
			    return true
		    end
	    end

	    return false
     end

     return
     {
            value = value
            isEqual = isEqual
     }, self
end

anURL = URL( "http://lua.org/"; )
anotherURL = URL( "http://lua.org/"; )
yetAnotherURL = URL( "http://www.lua.org/pil/"; )

anURL.isEqual( anotherURL )		// true
anURL.isEqual( yetAnotherURL )	// false

So far so good (?!?), but how do I integrate such an equality scheme in a Lua table? Especially if I use my object constructs as keys?

aTable = {}
aTable[ anURL ] = aContent
aTable[ anotherURL ] = aContent
aTable[ yetAnotherURL ] = anotherContent

I see that there are "Relational Metamethods":

http://www.lua.org/pil/13.2.html

But I'm not quite sure how I would hook up my objects into those... also... what about hashing? Usually, objects can/should/must provide their own hash value if they define their own equality... how would custom hashing and equality fit in the grand scheme of things, e.g. in a Lua table?

TIA!

Cheers

--
PA
http://alt.textdrive.com/