Reflect

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

Assignment Operators

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.

Call and Index Operators

Lifetime Management