Operator Overloading
The semantics of most operators provided by the language are defined within the language itself. For all built-in types, the language core defines the usual operators and user code may define additional operators to act on arbitrary user-defined types.
Note that Boolean logic operators (||, &&, !) cannot be redefined by
user code.
Example
struct S {
var ref: int*;
operator[copy]()
{
if (ref != null)
++*ref;
}
}
struct MyInt {
var value: int;
// method style (implicit "this" parameter)
operator[-](other: MyInt) => MyInt(this.value + other.value);
}
// function style (both operand parameters are explicit)
operator[+](a: MyInt, b: int) => MyInt(a.value + b);
operator[+](a: int, b: MyInt) => MyInt(a + b.value);
function test()
{
assert(MyInt(2) + 3 == MyInt(5));
}
Arithmetic Operators
operator[++](ref a): Prefix increment operator - the matching postfix operator is defined automaticallyoperator[--](ref a): Prefix decrement operator - the matching postfix operator is defined automaticallyoperator[+](a): Unary plus operator (+a)operator[-](a): Unary minus operator (-a)operator[~](a): Unary bit negation operator (~a)operator[+](a, b): Binary plus operator (a + b)operator[-](a, b): Binary minus operator (a - b)operator[*](a, b): Binary multiplication operator (a * b)operator[/](a, b): Binary division operator (a / b)operator[%](a, b): Binary modulus operator (a % b)operator[^^](a, b): Binary power operator (a ^^ b)operator[&](a, b): Binary AND operator (a & b)operator[|](a, b): Binary OR operator (a | b)operator[^](a, b): Binary XOR operator (a ^ b)operator[~](a, b): Binary concatenation operator (a ~ b)operator[<<](a, b): Binary left (bit) shift operator (a << b)operator[>>](a, b): Binary right (bit) shift operator (a >> b)operator[==](a, b): bool:operator[<>](a, b): int:
Assignment Operators
operator[=](ref a, b): Assignment operator (a = b)
Defining arithmetic assignment operators in addition to the corresponding
binary operators is possible, but not required. If no such operator is defined,
an expression such as a += b will be rewritten as a = a + b by the compiler.
operator[+=](ref a, b): Plus assignment (a += b)operator[-=](ref a, b): Minus assignment (a -= b)operator[*=](ref a, b): Multiply assignment (a *= b)operator[/=](ref a, b): Divide assignment (a /= b)operator[%=](ref a, b): Modulus assignment (a %= b)operator[^^=](ref a, b): Power assignment (a ^^= b)operator[&=](ref a, b): AND assignment (a &= b)operator[|=](ref a, b): OR assignment (a |= b)operator[^=](ref a, b): XOR assignment (a ^= b)operator[~=](ref a, b): Concatenation assignment (a ~= b)operator[<<=](ref a, b): Left shift assignment (a <<= b)operator[>>=](ref a, b): Right shift assignment (a >>= b)
Call and Index Operators
operator[call](object, arguments...): Call operator (object(arguments))operator[index](object, indices...): Index operator (object[arguments])
Lifetime Management
operator[copy](ref object): void: Post copy operator, called after a struct value got copied to a different memory locationoperator[move](ref object): void: Post move operator, called after a struct value got moved to a different memory locationoperator[cast](ref object, out target): void: Cast operator (cast(T)object) - supports@implicitannotation for implicit casts