lua-users home
lua-l archive

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



On Jan 31, 2005, at 05:27, Javier Guerra wrote:

The key equality is based on the table content. How?

not easy, in fact. you need __index and __newindex metamethods, of course. and lose the constructor syntax. (because values have to be added after
attaching the metatable)

Here is my feeble attempt so far:

-- instance method to compare a given object with this map for equality
        this.equals = function( anObject )
                if this == anObject then
                        return true
                end

                if this.isKindOf( anObject ) == true then
                        if this.size() == anObject.size() then
                                for aKey, aValue in ivars do
local anotherValue = anObject.get( aKey )

                                        if anotherValue ~= nil then
if this.isKindOf( anotherValue ) == true and aValue.equals( anotherValue ) == false then
                                                        return false
                                                end

if aValue ~= anotherValue then
                                                        return false
                                                end
                                        else
                                                return false
                                        end
                                end

                                return true
                        end
                end

                return false
        end


Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/