Operator Overloading
Operator methods give a user-defined type a meaning for an operation such as addition or indexing. Contracts can state the required operator signatures. Define the receiver, argument and result types explicitly; the same spelling does not make two different domain operations interchangeable.
A Complete Addition Example
This example defines component-wise addition. The receiver is borrowed and the result is a new value:
contract AddPoints {
op+(other: Self): Self;
}
struct Point: AddPoints {
x: i32,
y: i32,
}
fn (self: &Point) op+(other: Point): Point {
return Point { x: self.x + other.x, y: self.y + other.y };
}
fn main(): i32 {
let first = Point { x: 1, y: 2 };
let second = Point { x: 3, y: 4 };
let sum = first + second;
if sum.x != 4 || sum.y != 6 { return 1; }
$println(sum.x);
$println(sum.y);
return 0;
}Save it as point-add.vx and run vex run point-add.vx. Expected output is 4 then 6, with exit status 0. The method is declared outside the struct, using the same receiver syntax as ordinary methods. The contract name has no $ prefix.
Signatures and Contracts
An operator method has the same ownership obligations as an ordinary call:
self: &Tborrows the receiver for observation;self: &T!requests mutable access;self: Treceives the value according to its move/copy behavior;- a returned reference must remain tied to valid storage.
A contract declaration and its implementation must agree on receiver mutability, parameters and return type. Merely using a familiar name such as Add or Index does not establish that agreement. See Contracts for an example that calls through a bound.
Operator Method Forms
| Source operation | Method spelling | Design question |
|---|---|---|
left + right | op+ | Does addition return the same type or another domain value? |
left - right | op- with an operand | Is subtraction meaningful for every pair? |
-value | Unary op- | How are overflow and signedness handled? |
left * right | op* | Is this scaling, component-wise multiplication or composition? |
left == right | op== | What equality law does the type promise? |
value[index] | op[] | Is the result a value or a reference, and how are bounds checked? |
value[index] = replacement | Mutable indexing / setter path | Does the selected callable return a mutable reference or accept a replacement? |
The compiler selects an applicable callable using the actual receiver and operand types. Cross-type right operands are possible; make overload selection clear with explicit numeric types when otherwise ambiguous. Do not infer an operation or an automatic conversion merely from a table of conventional contract names.
Index Operators
An index reader can borrow storage owned by the receiver; a separate op[]= method defines assignment. The following example checks assignment and subsequent reads:
struct Buffer {
data: [i32; 3],
}
fn (self: &Buffer) op[](index: i32): &i32 {
return &self.data[index];
}
fn (self: &Buffer!) op[]=(index: i32, value: i32) {
self.data[index] = value;
}
fn main(): i32 {
let! buffer = Buffer { data: [10, 20, 30] };
buffer[1] = 42;
if buffer[0] != 10 || buffer[1] != 42 || buffer[2] != 30 { return 1; }
$println(buffer[1]);
return 0;
}Save as indexed-buffer.vx and run vex run indexed-buffer.vx. Expected output is 42, with exit status 0. The method delegates bounds checking to the array access. It does not make arbitrary indices valid, and it must not return a reference to a temporary or unrelated local.
Indexing uses the selected source callable. Prelude containers and user-defined types share this path; their names do not prove contiguous storage. Intrinsic array, slice, pointer and tensor forms have separate structural lowering.
Equality and Ordering
Define what comparison means for the domain before adding operators. For a version number, lexicographic comparison of (major, minor) is different from comparing only major. For floating-point fields, NaN can make an apparently simple ordering partial. The compiler accepting the method does not prove reflexivity, transitivity or consistency between equality and ordering.
Test equal values, different values and boundary cases. Keep all supplied comparison operators consistent; do not assume missing methods are derived unless the selected contract explicitly provides them.
Mutation and Compound Assignment
Operations that mutate a receiver need the corresponding mutable receiver contract. A mutable local binding alone does not change an immutable method's signature. For a compound assignment, verify which overload is selected and whether the right operand is moved, copied or borrowed.
Avoid placeholder setters that silently do nothing. If an operation can fail, choose an API that can report that failure instead of disguising it behind an operator whose callers expect an ordinary result.
Cross-Type Operations
Scaling a vector by a scalar and multiplying two vectors are separate signatures. Give each an intentional result type and document its units. A named method is often clearer when the meaning is unusual or the operation needs configuration, error handling or more than one argument.
Validation and Implementation Notes
The repository contains operator and indexing fixtures under tests/07_contracts/operators/, including complex_arith_001.vx, index_001.vx and index_mut_001.vx. Those sources describe test cases; their presence alone does not certify every generic, variadic or cross-target combination. Validate the overloads used by a public API with the actual compiler and deployment targets.