| AngelScript

Operator overloads

It is possible to define what should be done when an operator is used with a script class. While not necessary in most scripts it can be useful to improve readability of the code.

This is called operator overloading, and is done by implementing specific class methods. The compiler will recognize and use these methods when it compiles expressions involving the overloaded operators and the script class.

# Prefixed unary operators

op | opfunc
- | opNeg
~ | opCom
++ | opPreInc
-- | opPreDec

When the expression `op a` is compiled, the compiler will rewrite it as `a.opfunc()` and compile that instead.

# Postfixed unary operators

op | opfunc
++ | opPostInc
-- | opPostDec

When the expression `a op` is compiled, the compiler will rewrite it as `a.opfunc()` and compile that instead.

# Comparison operators

op | opfunc
== | opEquals
!= | opEquals
< | opCmp
<= | opCmp
> | opCmp
>= | opCmp

The `a == b` expression will be rewritten as `a.opEquals(b)` and `b.opEquals(a)` and then the best match will be used. `!=` is treated similarly, except that the result is negated. The opEquals method must be implemented to return a `bool` in order to be considered by the compiler.

The comparison operators are rewritten as `a.opCmp(b) op 0` and `0 op b.opCmp(a)` and then the best match is used. The opCmp method must be implemented to return a `int` in order to be considered by the compiler. If the method argument is to be considered larger than the object then the method should return a negative value. If they are supposed to be equal the return value should be 0.

If an equality check is made and the opEquals method is not available the compiler looks for the opCmp method instead. So if the opCmp method is available it is really not necesary to implement the opEquals method, except for optimization reasons.

# Assignment operators

op | opfunc
= | opAssign
+= | opAddAssign
-= | opSubAssign
*= | opMulAssign
/= | opDivAssign
%= | opModAssign
**= | opPowAssign
&= | opAndAssign
|= | opOrAssign
^= | opXorAssign
<<= | opShlAssign
>>= | opShrAssign
>>>= | opUShrAssign

The assignment expressions `a op b` are rewritten as `a.opfunc(b)` and then the best matching method is used. An assignment operator can for example be implemented like this:

      obj@ opAssign(const obj &in other)
      {
        // Do the proper assignment
        ...

        // Return a handle to self, so that multiple assignments can be chained
        return this;
      }

All script classes have a default assignment operator that does a bitwise copy of the content of the class, so if that is all you want to do, then there is no need to implement this method.

# Binary operators

op | opfunc | opfunc_r
+ | opAdd | opAdd_r
- | opSub | opSub_r
* | opMul | opMul_r
/ | opDiv | opDiv_r
% | opMod | opMod_r
** | opPow | opPow_r
& | opAnd | opAnd_r
| | opOr | opOr_r
^ | opXor | opXor_r
<< | opShl | opShl_r
>> | opShr | opShr_r
>>> | opUShr | opUShr_r

The expressions with binary operators `a op b` will be rewritten as `a.opfunc(b)` and `b.opfunc_r(a)` and then the best match will be used.

# Index operators

op | opfunc
[] | opIndex

When the expression `a[i]` is compiled, the compiler will rewrite it as `a.opIndex(i)` and compile that instead. Multiple arguments between the brackets is also supported.

The index operator can also be formed similarly to property accessors. The get accessor should then be named `get_opIndex` and have one parameter for the indexing. The set accessor should be named `set_opIndex` and have two parameters, the first is for the indexing, and the second for the new value.

      class MyObj
      {
        float get_opIndex(int idx) const       { return 0; }
        void set_opIndex(int idx, float value) { }
      }

When the expression `a[i]` is used to retrieve the value, the compiler will rewrite it as `a.get_opIndex(i)`. When the expression is used to set the value, the compiler will rewrite it as `a.set_opIndex(i, expr)`.

# Functor operator

op | opfunc
() | opCall

When the expression `expr(arglist)` is compiled and expr evaluates to an object, the compiler will rewrite it as `expr.opCall(arglist)` and compile that instead.

# Type conversion operators

op | opfunc
type(expr) | opConv, opImplConv

When the expression `type(expr)` is compiled and type doesn't have a constructor that take an argument with the type of the expression, the compiler will try to rewrite it as `expr.opConv()`. The compiler will then chose the opConv that returns the desired type.

By implementing the opImplConv instead of opConv, the compiler will be able to use this for implicit conversions too, e.g. when compiling an assignment or a function argument.

      class MyObj
      {
        double myValue;
        double opImplConv() const  { return myValue; }
        int    opConv() const      { return int(myValue); }
      }

This should only be used for value conversions and not reference casts. That is, the methods are expected to return a new instance of the value with the new type.

See Also
  Type conversions