gopher-lua icon indicating copy to clipboard operation
gopher-lua copied to clipboard

__lt and __le can't compare table with number ,but it can be in lua5.1

Open rhettli opened this issue 4 years ago • 1 comments

  • [x] GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read Lua 5.1 reference manual carefully?
  • [x] GopherLua is a Lua5.1 implementation. In Lua, to keep it simple, it is more important to remove functionalities rather than to add functionalities unlike other languages . If you are going to introduce some new cool functionalities into the GopherLua code base and the functionalities can be implemented by existing APIs, It should be implemented as a library.

Please answer the following before submitting your issue:

  1. What version of GopherLua are you using? :
  2. What version of Go are you using? :
  3. What operating system and processor architecture are you using? :win
  4. What did you do? : use metatable to compare table with number
  5. What did you expect to see? :
  6. What did you see instead? :

metatable func __lt and __le not work

local b = setmetatable({ _val = 10 }, {
    __le = function(n, m)
        local x = type(n) == 'table' and n._val or m._val
        local val = type(n) ~= 'table' and n or m
        print('__le:', x, val)
        return x <= val
    end,
    __lt = function(n, m)
        print('__lt:', n,m)
        return 1
    end,
})

and

print(b<1) -- it will report an error

but it can be run in lua5.1,see the pic bellow

Screenshot 2021-04-27 164610

rhettli avatar Apr 27 '21 08:04 rhettli

Invoking __lt and __le for mixed types is a 5.2+ feature.

tongson avatar Jun 06 '21 11:06 tongson