Skip to content

Operators

Declaring Operators

Operators are declared directly using their symbol as the function name — no operator keyword needed:

type Vector2
{
float x;
float y;
Vector2 +(Vector2 other)
{
return Vector2(x + other.x, y + other.y);
}
Vector2 -(Vector2 other)
{
return Vector2(x - other.x, y - other.y);
}
Vector2 *(float scalar) Vector2(x * scalar, y * scalar);
}
Vector2 a = Vector2(1.0, 2.0);
Vector2 b = Vector2(3.0, 4.0);
Vector2 c = a + b;

Binary Operators

T +(T other) // addition
T -(T other) // subtraction
T *(T other) // multiplication
T /(T other) // division
T %(T other) // modulo

Comparison Operators

Comparison operators return bool:

bool ==(T other)
bool !=(T other)
bool <(T other)
bool >(T other)
bool <=(T other)
bool >=(T other)

Example:

type Point
{
int x;
int y;
bool ==(Point other)
{
return this.x == other.x && this.y == other.y;
}
bool <(Point other)
{
if (this.x != other.x) { return this.x < other.x; }
return this.y < other.y;
}
}
Point a = Point(1, 2);
Point b = Point(1, 2);
Console.WriteLine($"{a == b}"); // true

Unary Operators

T -() // unary negation
bool !() // logical NOT
T ~() // bitwise NOT

Pointer Operators

These built-in operators work on raw pointers and require an unsafe block. They cannot be overloaded.

unsafe
{
int x = 42;
int& p = &x; // &x — address-of (produces T&)
int val = *p; // *p — dereference (reads through pointer)
*p = 100; // *p — dereference (writes through pointer)
p->field; // -> — member access through pointer
}

See Raw Pointers for full details.

Indexer []

The indexer uses property-style accessors: get, set, add, sub:

type MyArr
{
int a;
int b;
int [](int index)
{
get
{
if (index == 0) { return this.a; }
return this.b;
}
set
{
if (index == 0) { this.a = value; }
if (index == 1) { this.b = value; }
}
add
{
if (index == 0) { this.a = this.a + value; }
if (index == 1) { this.b = this.b + value; }
}
sub
{
if (index == 0) { this.a = this.a - value; }
if (index == 1) { this.b = this.b - value; }
}
}
}
MyArr m;
m[0] = 10;
m[1] = 20;
m[0] += 5;

Copy and Move Operators

Define custom copy and move assignment as overloads of operator= with copy or move parameter modifiers. copy is immut ref (cannot modify source), move is mutable ref:

type Handle
{
int value;
void =(copy Handle other)
{
this.value = other.value;
}
void =(move Handle other)
{
this.value = other.value;
other.value = -1;
}
}

Use : default or : delete to auto-generate or disable:

type Data
{
void =(copy) : default;
void =(move) : default;
}

Plain operator= (without copy/move) is also supported for custom assignment:

type Wrapper
{
void =(Wrapper other) { this.val = other.val; }
void =(ref Wrapper other) { this.val = other.val; }
}