• Subject: Re: metamethods... are __gt and __ge missing ?
• From: Philipp Janda <siffiejoe@...>
• Date: Sun, 22 Sep 2013 14:53:40 +0200

```Am 22.09.2013 14:10 schröbte Jayanth Acharya:
```
```
So, if I understand this correctly, I cannot use the < and <= operators in
Lua code while comparing 2 tables say 'a' and 'b', where the intent was to
check (a < b). However I could rewrite the Lua code as (b > a). Right ?

If so, then this is semantically correct, but makes the code less readable,
no ?

```
```    _________________________________________
|  you have   | you write |   Lua uses   |
|-------------|-----------|--------------|
| __lt only   |   a < b   |    a < b     |
| __lt only   |   a <= b  | not (b < a)  |
| __lt only   |   a > b   |    b < a     |
| __lt only   |   a >= b  | not (a < b)  |
| __lt + __le |   a < b   |    a < b     |
| __lt + __le |   a <= b  |    a <= b    |
| __lt + __le |   a > b   |    b < a     |
| __lt + __le |   a >= b  |    b <= a    |
| __le only   |   a < b   |   #error#    |
| __le only   |   a <= b  |    a <= b    |
| __le only   |   a > b   |   #error#    |
| __le only   |   a >= b  |    b <= a    |
------------------------------------------

HTH,
Philipp

```
```#!/usr/bin/lua

local meta = {}
local function lt( a, b )
print( "LT", a[ 1 ], b[ 1 ] )
return a[ 2 ] < b[ 2 ]
end
local function le( a, b )
print( "LE", a[ 1 ], b[ 1 ] )
return a[ 2 ] <= b[ 2 ]
end

local a = setmetatable( { "a", 1 }, meta )
local b = setmetatable( { "b", 2 }, meta )

local function try( f )
if not pcall( f ) then print( "#error#" ) end
end

function test()
try( function() return a < b end )
try( function() return a <= b end )
try( function() return a > b end )
try( function() return a >= b end )
end

meta.__lt = lt
test()
meta.__le = le
test()
meta.__lt = nil
test()

```