lua-users home
lua-l archive

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

Hi list,

I encountered the following odd behavior while toying with functionality of an application I'm writing (created a semi-minimal test case):

> a = {}; b = {};
> mt = {__eq = function() return 2; end};
> setmetatable(a, mt); setmetatable(b, mt);
> return a == b;
> assert(getmetatable(a) == getmetatable(b) and type(getmetatable(a).__eq)=='function')

I would expect "return a==b" to return 2, not true. The assertion passed.

We can tell that the __eq metamethod is obviously being called as naturally a does not equal b. It appears that the return value of __eq is being cast to a boolean. According to the manual:

     function getcomphandler (op1, op2, event)
       if type(op1) ~= type(op2) then return nil end
       local mm1 = metatable(op1)[event]
       local mm2 = metatable(op2)[event]
       if mm1 == mm2 then return mm1 else return nil end

The "eq" event is defined as follows:

     function eq_event (op1, op2)
       if type(op1) ~= type(op2) then  -- different types?
         return false   -- different objects
       if op1 == op2 then   -- primitive equal?
         return true   -- objects are equal
       -- try metamethod
       local h = getcomphandler(op1, op2, "__eq")
       if h then
         return (h(op1, op2))
         return false

I see that it should be executing the line "return (h(op1, op2))" but that's not what I seem to get in response. I would expect "true" to be the result if the manual stated "return not not (h(op1, op2))" but it doesn't.

Is this a manual problem, a Lua problem, or am I missing something?

-- Matthew P. Del Buono