All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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

- 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

++ opPostInc
-- opPostDec

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

Comparison operators

== 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

= 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

+ 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

[] 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

() 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

type(expr)opConv, opImplConv
cast<type>(expr)opCast, opImplCast

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.

If a reference cast is desired, i.e. a different type of handle to the same object instance, then the opCast method should be implemented instead. The compiler will attempt to rewrite an expression cast<type>(expr) as expr.opCast(), and chose the opCast overload that returns a handle of the desired type. Here too the opImplCast can be implemented instead if the reference cast is allowed to be performed implicitly by the compiler.

  class MyObjA
    MyObjB @objB;
    MyObjC @objC;
    MyObjB @opCast() { return objB; }
    MyObjC @opImplCast() { return objC; }
    const MyObjB @opCast() const { return objB; }
    const MyObjC @opImplCast() const { return objC; }

An example where the opCast/opImplCast operator overloads come in handy is when extending a type without directly inheriting from it.

See Also
Type conversions, Inheriting from application registered class