*__div()*
------
the `/` operation. Behavior similar to the "add" operation.
"mod": *__mod()*
------
the `%` operation. Behavior similar to the "add" operation, with the
operation `o1 - floor(o1/o2)*o2` as the primitive operation.
"pow": *__pow()*
------
the `^` (exponentiation) operation. Behavior similar to the "add" operation,
with the function `pow` (from the C math library) as the primitive operation.
"unm": *__unm()*
------
the unary `-` operation.
>lua
function unm_event (op)
local o = tonumber(op)
if o then -- operand is numeric?
return -o -- `-` here is the primitive `unm`
else -- the operand is not numeric.
-- Try to get a handler from the operand
local h = metatable(op).__unm
if h then
-- call the handler with the operand
return h(op)
else -- no handler available: default behavior
error(...)
end
end
end
<
"concat": *__concat()*
---------
the `..` (concatenation) operation.
>lua
function concat_event (op1, op2)
if (type(op1) == "string" or type(op1) == "number") and
(type(op2) == "string" or type(op2) == "number") then
return op1 .. op2 -- primitive string concatenation
else
local h = getbinhandler(op1, op2, "__concat")
if h then
return h(op1, op2)
else
error(...)
end
end
end
<
"len": *__len()*
------
the `#` operation.
>lua
function len_event (op)
if type(op) == "string" then
return strlen(op) -- primitive string length
elseif type(op) == "table" then
return #op -- primitive table length
else
local h = metatable(op).__len
if h then
-- call the handler with the operand
return h(op)
else -- no handler available: default behavior
error(...)
end
end
end
<
"eq": *__eq()*
-----
the `==` operation.
The function `getcomphandler` defines how Lua chooses a metamethod for
comparison operators. A metamethod only is selected when both objects being
compared have the same type and the same metamethod for the selected
operation.
>lua
function getcomphandler (op1, op2, event)
if type(op1) ~= type(op2) then return nil end
local mm1 = metatable(op1)[event]
local mm2 = metatable(op2)[event]
if mm1 == mm2 then return mm1 else return nil end
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,