SuperC documentation

Operator overload

You can map an operator (+, +=, etc) to a type method call.

This can be very useful to avoid a headache when working with complex data types like strings and arrays.

But this is one of the features that could kill a C succesor, and has so many detractors because of the hidden code; so it has to be implemented wisely.

There are some rules that the compiler follows:

Binary arithmetic operators

Operator Method
+ __add__(self, other)
- __sub__(self, other)
* __mul__(self, other)
/ __div__(self, other)
% __mod__(self, other)

Comparison operators

Operator Method
< __lt__(self, other)
> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)

Assignment operators

Operator Method
+= __iadd__(self, other)
-= __isub__(self, other)
*= __imul__(self, other)
/= __idiv__(self, other)
%= __imod__(self, other)

Unary operators

Operator Method
- __neg__(self)
+ __pos__(self)
~ __del__(self)

Note: operator ~ is mostly used as object destructor, rather than arithmetic inversion. That is why it is called __del__ instead of __inv__.

Examples


Previous

Type methods

Next

Function overload
Playground