lua-users home
lua-l archive

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

Am 26.07.2013 17:17 schröbte Andrew Starks:

Within my own Lua code, I often want to turn an object into a plain
number. The meta-methods can effectively do most of what I want, but
not when I'm dealing with relational operators when one side of is an
actual number.

Finally, __tonumber just feels... missing. I even had to triple check
that it wasn't in 5.2, lest I embarrass myself. :)

But Lua 5.2 *does* have support for mixed arguments in relational operations :-p

    local o = { n = 1 }
    local mt = {
      __lt = function( a, b )
        if type( a ) == "table" then a = a.n end
        if type( b ) == "table" then b = b.n end
        return a < b
    setmetatable( o, mt )

    if o < 2 then
      print( "o < 2" )
    if o >= 1 then
      print( "o >= 1" )