pub trait comparison {
// Provided methods
fn __eq(&self, other: any) -> boolean { ... }
fn __lt(&self, other: any) -> boolean { ... }
fn __le(&self, other: any) -> boolean { ... }
}Expand description
Metamethods controlling the comparison operators.
Note that a > b is translated to b < a and a >= b to b <= a,
so only three events exist.
Provided Methods§
Sourcefn __eq(&self, other: any) -> boolean
fn __eq(&self, other: any) -> boolean
The equal (==) operation. Only invoked when the values being
compared are both tables or both full userdata and they are not
primitively equal. The result is always converted to a boolean.
§Examples
local mt = { __eq = function(a, b) return a.x == b.x and a.y == b.y end }
local p = setmetatable({ x = 1, y = 2 }, mt)
local q = setmetatable({ x = 1, y = 2 }, mt)
print(p == q) --> true (same coordinates, different tables)Sourcefn __lt(&self, other: any) -> boolean
fn __lt(&self, other: any) -> boolean
The less than (<) operation.
§Examples
local mt = { __lt = function(a, b) return a.len < b.len end }
local short = setmetatable({ len = 1 }, mt)
local long = setmetatable({ len = 9 }, mt)
print(short < long) --> true
print(long > short) --> true (a > b runs __lt(b, a))Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".