Home Explore Blog CI



neovim

18th chunk of `runtime/doc/luaref.txt`
c9296d72c62b0a6edf65058b5dbc698cf8a9509aee04801d0000000100000fa0
     end
<
The "eq" event is defined as follows:
>lua
       function eq_event (op1, op2)
         if type(op1) ~= type(op2) then  -- different types?
           return false   -- different objects
         end
         if op1 == op2 then   -- primitive equal?
           return true   -- objects are equal
         end
         -- try metamethod
         local h = getcomphandler(op1, op2, "__eq")
         if h then
           return h(op1, op2)
         else
           return false
         end
       end
<
`a ~= b` is equivalent to `not (a == b)`.

"lt":                                                                   *__lt()*
-----
the `<` operation.
>lua
       function lt_event (op1, op2)
         if type(op1) == "number" and type(op2) == "number" then
           return op1 < op2   -- numeric comparison
         elseif type(op1) == "string" and type(op2) == "string" then
           return op1 < op2   -- lexicographic comparison
         else
           local h = getcomphandler(op1, op2, "__lt")
           if h then
             return h(op1, op2)
           else
             error(...);
           end
         end
       end
<
`a > b` is equivalent to `b < a`.

"le":                                                                   *__le()*
-----
the `<=` operation.
>lua
       function le_event (op1, op2)
         if type(op1) == "number" and type(op2) == "number" then
           return op1 <= op2   -- numeric comparison
         elseif type(op1) == "string" and type(op2) == "string" then
           return op1 <= op2   -- lexicographic comparison
         else
           local h = getcomphandler(op1, op2, "__le")
           if h then
             return h(op1, op2)
           else
             h = getcomphandler(op1, op2, "__lt")
             if h then
               return not h(op2, op1)
             else
               error(...);
             end
           end
         end
       end
<
`a >= b` is equivalent to `b <= a`. Note that, in the absence of a "le"
metamethod, Lua tries the "lt", assuming that `a <= b` is equivalent
to `not (b < a)`.

"index":                                                             *__index()*
--------
The indexing access `table[key]`.
>lua
       function gettable_event (table, key)
         local h
         if type(table) == "table" then
           local v = rawget(table, key)
           if v ~= nil then return v end
           h = metatable(table).__index
           if h == nil then return nil end
         else
           h = metatable(table).__index
           if h == nil then
             error(...);
           end
         end
         if type(h) == "function" then
           return h(table, key)      -- call the handler
         else return h[key]          -- or repeat operation on it
       end
<
"newindex":                                                       *__newindex()*
-----------
The indexing assignment `table[key] = value`.
>lua
       function settable_event (table, key, value)
         local h
         if type(table) == "table" then
           local v = rawget(table, key)
           if v ~= nil then rawset(table, key, value); return end
           h = metatable(table).__newindex
           if h == nil then rawset(table, key, value); return end
         else
           h = metatable(table).__newindex
           if h == nil then
             error(...);
           end
         end
         if type(h) == "function" then
           return h(table, key,value)    -- call the handler
         else h[key] = value             -- or repeat operation on it
       end
<
"call":                                                               *__call()*
-------
called when Lua calls a value.
>lua
       function function_event (func, ...)
         if type(func) == "function" then
           return func(...)   -- primitive call
         else
           local h = metatable(func).__call
           if h then
             return h(func, ...)
           else
             error(...)
         

Title: Lua Metamethods for Relational Operations, Indexing, and Function Calls
Summary
This section explains the Lua metamethods for relational operations (less than, less than or equal to), indexing (accessing and assigning table elements), and function calls. It provides Lua code that details how each operation is handled, including checks for operand types, leveraging metamethods when available, and employing fallback mechanisms (like converting `a > b` to `b < a` or using `__lt` when `__le` is absent). It covers primitive comparisons for numbers and strings and also delves into how metamethods are used to customize table access and assignment behavior, as well as how function call behavior can be modified through metamethods.