lua-users home
lua-l archive

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


>>No roadmap, published or otherwise. Do you have any special requests??

One thing I'd like to see is a relaxation of the __eq metamethod invocation
rules. The Lua core currently considers any objects that are not the same
type to be different. This is fine except in the context of a language
extension that introduces new typed objects that have similar semantics to
the internal ones. In my case I have an object that is similar to a string,
so constructs like this are meaningful:

a = m.str('text')
b = 'text'
if a == b then ....

But the Lua core yields false for a==b which is intuitively unexpected (by
the users of our system) and leads to hard to find bugs.

Given the other discussions I've seen of providing "primitives not policies"
I'd like to suggest the __eq metamethod invocation is modified to something
similar to this:

function getcomphandler (op1, op2, event)
   local mm1 = metatable(op1)[event]
   local mm2 = metatable(op2)[event]
   return mm1 or mm2
end

The "eq" event is defined as follows:
 function eq_event (op1, op2)
   if type(op1) == type(op2) and op1 == op2 then   -- primitive equal?
     return true   -- objects are equal
   end
   -- try metamethod
   local h = getcomphandler(op1, op2, "__eq")
   if h then
     return h(op1, op2)
   else
     return false
   end
 end

The 'identity' requirement is then a programmer choice in the __eq method,
e.g.

object.__eq function(op1,op2)
  if type(op1) ~= type(op2) then return false end
  ...

I can see there could be an issue if both objects of different types define
a (conflicting) __eq metamethod but this could be resolvable by some
priority convention

Or perhaps have another metamethod (__same?) that is looser, or another
equality operator that is looser (:=?).