lua-users home
lua-l archive

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


The reason is speed. There are several cases in Lua where metamethods are not called at all, which saves one or more table lookups when the metamethods are not used. The drawback is that when you want to use them you can't.

Workarounds:
- you can simply call yourself the comparison function instead of using the operator:
if foometatable.eq(foovar, 32) then foofunc() end
- you can cast one of your values to the same type as the other before the comparison operator is evaluated
if foovar==footype.new(32) then foofunc() end

-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Sam Roberts
Envoyé : 2 octobre 2006 15:08
À : Lua list
Objet : why is comparison not handled like other binary operatorsw/metamethods?


Particularly, if I have a user-data with __mul in it's metadata, I can multiply it with a lua_Number.

But if I have a user-data with __eq int it's metadata, I can't compare it with a lua_Number.

There must be a reason... what is it?

Also, what is the suggested work-around? The user-data wraps a uint64, for those interested.

This is with 5.0.

Thanks,
Sam