Define operator overloads for classes, enabling custom operator behavior for mathematical and comparison operations.
Syntax
--- @operator <operator> ( <operand_types> ) : <return_type>
Supported Operators
Operator Symbol Description add+Addition sub-Subtraction mul*Multiplication div/Division mod%Modulo pow^Exponentiation unm-Unary minus concat..Concatenation len#Length eq==Equality lt<Less than le<=Less than or equal
Examples
Vector Class with Operators
--- @class Vector
--- @field x number
--- @field y number
--- @operator add ( Vector ) : Vector
--- @operator sub ( Vector ) : Vector
--- @operator mul ( number ) : Vector
--- @operator div ( number ) : Vector
--- @operator unm : Vector
--- @operator len : number
--- @operator eq ( Vector ) : boolean
local Vector = {}
function Vector . new ( x , y )
return setmetatable ({ x = x or 0 , y = y or 0 }, {
__index = Vector ,
__add = Vector . __add ,
__sub = Vector . __sub ,
__mul = Vector . __mul ,
__div = Vector . __div ,
__unm = Vector . __unm ,
__len = Vector . __len ,
__eq = Vector . __eq
})
end
function Vector : __add ( other )
return Vector . new ( self . x + other . x , self . y + other . y )
end
function Vector : __mul ( scalar )
return Vector . new ( self . x * scalar , self . y * scalar )
end
function Vector : __unm ()
return Vector . new ( - self . x , - self . y )
end
function Vector : __len ()
return math.sqrt ( self . x * self . x + self . y * self . y )
end
Matrix Class with Operator Overloading
--- @class Matrix
--- @field data number[][]
--- @operator add ( Matrix ) : Matrix
--- @operator sub ( Matrix ) : Matrix
--- @operator mul ( Matrix ) : Matrix
--- @operator mul ( number ) : Matrix
local Matrix = {}
function Matrix : __mul ( other )
if type ( other ) == "number" then
-- Scalar multiplication
local result = {}
for i = 1 , # self . data do
result [ i ] = {}
for j = 1 , # self . data [ i ] do
result [ i ][ j ] = self . data [ i ][ j ] * other
end
end
return Matrix . new ( result )
else
-- Matrix multiplication
local result = {}
for i = 1 , # self . data do
result [ i ] = {}
for j = 1 , # other . data [ 1 ] do
result [ i ][ j ] = 0
for k = 1 , # other . data do
result [ i ][ j ] = result [ i ][ j ] + self . data [ i ][ k ] * other . data [ k ][ j ]
end
end
end
return Matrix . new ( result )
end
end
Complex Number Class
--- @class Complex
--- @field real number
--- @field imag number
--- @operator add ( Complex ) : Complex
--- @operator sub ( Complex ) : Complex
--- @operator mul ( Complex ) : Complex
--- @operator div ( Complex ) : Complex
--- @operator unm : Complex
--- @operator eq ( Complex ) : boolean
local Complex = {}
function Complex . new ( real , imag )
return setmetatable ({ real = real or 0 , imag = imag or 0 }, {
__index = Complex ,
__add = Complex . __add ,
__mul = Complex . __mul ,
__eq = Complex . __eq
})
end
function Complex : __add ( other )
return Complex . new ( self . real + other . real , self . imag + other . imag )
end
function Complex : __mul ( other )
return Complex . new (
self . real * other . real - self . imag * other . imag ,
self . real * other . imag + self . imag * other . real
)
end
Usage Examples
local v1 = Vector . new ( 1 , 2 )
local v2 = Vector . new ( 3 , 4 )
local v3 = v1 + v2 -- Vector addition: (4, 6)
local v4 = v2 - v1 -- Vector subtraction: (2, 2)
local v5 = v1 * 2 -- Scalar multiplication: (2, 4)
local v6 = - v1 -- Unary minus: (-1, -2)
local len = # v1 -- Vector length: ~2.24
local equal = v1 == v2 -- Equality check: false
local c1 = Complex . new ( 1 , 2 )
local c2 = Complex . new ( 3 , 4 )
local c3 = c1 + c2 -- Complex addition: 4 + 6i
local c4 = c1 * c2 -- Complex multiplication: -5 + 10i
Operator overloads provide type-safe, intuitive syntax for mathematical and custom operations on your classes.
Features
Custom Operators Define custom behavior for mathematical operators
Type Safety Type-safe operations with proper return types
Multiple Operands Support different operand types per operator
Comparison Ops Implement equality and comparison operations