You can define new operators in Nosica. Here's the list of operators you can overload :

- +
- *
- -
- /
- %
- ^
- +=
- *=
- -=
- /=
- %=
- ^=
- !
- ~
- ~~
- !~
- >>
- <<

### Operators syntax

## Infix operators

Infix operators are operators that applies on the left member of the expression.

ResultType infix OperatorName(Argument arg) [const] { }

Example :

int infix ~~(const int rhs) const { }

Thus, when you write :

int i = 0; int j = 0; if (i ~~ j) { }

You're in fact writing :

int i = 0; int j = 0; if (i.infix ~~(j)) { }

### Postfix operators

Postfix operators are operators that applies on the right member of the expression.

ResultType postfix OperatorName(Argument arg) [const] { }

Example :

class Vector { Matrix postfix *(const Matrix rhs) const { } }

Thus, when you write :

Matrix i; Vector j; Matrix k = i * j;

You're in fact writing :

Matrix i; Vector j; Matrix k = j.postfix *(i);

This is usefull when wanting to extends an existing class without breaking the protection mechanism. Suppose you have defined * infix operators in the Matrix class. Suppose now that you write a Vector class but you don't want to modify the Matrix class to handle multiplication. The answer is handy : simply write a postfix operator.

### Prefix operators

There is only 2 prefix operators :

- -
- !

Syntax :

ResultType prefix OperatorName() const { }

Example :

int prefix -() const { }

Thus, when you write :

int i = 0; int j = -i;

You're in fact writing :

int i = 0; int j = i.prefix -();

### A notes on instance operators

Operators ~, ~~, and !~ are called instance operators. They are in fact the copy operator and comparison operators.

See this document for a spec on value/instance operators.

In brief : For reference instances :

- Operators =, == and != always exists. They are called the value operators. They apply on the reference itself, not the instance. This way you can assign a reference on an instance, or compare references to see if they are pointing on the same instance
- Operators ~, ~~, and != exists only when you define them. The semantic is instance copy and instance comparison.

For primitive instances :

- Operators =, == and != only exists when you define the correspondant ~, ~~ and != instance operators. Because for a primitive type, value and instance are the same.
- Operators ~, ~~ and !~ only exists when you define them. The semantic is instance copy and instance comparison.

No comments yet