lua-users home
lua-l archive

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



I have an object system that looks to Lua like userdata objects. These objects encapsulate many different types, some strings, some numbers, some objects that can be placed in a hierarchy to do some rendering. I place simple types like floating point numbers into objects because they have special behavior: specifically firing events into the node objects that contain them.

I want to be able to do simple operations on these objects, specifically on the numeric or string ones. So I added metamethods for things like __lt and __mul. For the arithmetic functions like __mul, everything works fine. If either of the operands is not a number it will call __mul, first trying the left op and then the right. So I can go:

    local a = SFFloat(5.2) -- a contains a floating pt. object
                           -- with the number 5.2 in it
    local c = a * 4.7

and my __mul function gets called. I get my object from the passed userdata, convert it to a number, do the mul and return a number. Great!

But this does not work for __lt. If you look at luaV_lessthan() you can see that it is because both operands must be of the same type and have the same metamethod. It seems like the same logic could be used (call either metamethod, preferring the one for the left operand). Is there a reason it was done this way? I looked at both 5.02 and work6 and they were both the same.

--
chris marrin                ,""$,
cmarrin@arch.sel.sony.com b`    $                             ,,.
(408) 955-3049          mP     b'                            , 1$'
Sony    ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'