Operator Overloading

From The Elements Wiki
Jump to: navigation, search

This is an Oxygene Language topic
 

Oxygene Language: Intro | Structured Overview | Grammar | Keywords | Functions



Oxygene supports operator overloading on classes and records. The syntax for operator overloading is:

// unary:
class operator <name>(val: MyType): MyType;

// binary:
class operator <name>(val1, val2: MyType): MyType;

// cast:
class operator explicit(val: MyType): MyType;
class operator implicit(val: MyType): MyType;

Supported operators

Name
Type
Example
Description

Plus

Unary

x := +5;

Unary "plus" operator. Usually does nothing for number types but can be overridden.

Minus

Unary

x := -5;

Unary "minus" operator, the operator to negate a value

BitwiseNot

Unary

x := not x;

Bitwise not is used to invert the bits

Increment

Unary

inc(x);

Increment, never called from Oxygene code but is used for when i++ is used from C#.

Decrement

Unary

dec(x);

Decrement, never called from Oxygene code but is used for when i-- is used from C#.

Implicit

Cast

var x: TypeX; y: TypeY;


x := y;

Implicit overload; makes two unrelated types assignment compatible with or without a cast. The input type is the type of the supported input value. The output type is the result type.

Explicit

Cast

var x: TypeX; y: TypeY;


x := y as TypeX;

Explicit overload; makes two unrelated types cast compatible. The input type is the type of the supported input value. The output type is the result type.

Add

Binary

x := 1+2;

Binary addition; add two values to each other and return the result

Subtract

Binary

x := 2-3;

Binary subtraction; subtract two values to each other and return the result

Multiply

Binary

x := 3*4;

Multiply two values and return the result.

Divide

Binary

x := 4 div 5;

Divide two values and return the result.

Modulus

Binary

x := 5 mod 6;

Divide and returns the modulus (what's left after a division).

BitwiseAnd

Binary

x := $04 and $ff

binary and operator

BitwiseOr

Binary

x := $04 or $ff

binary or operator.

BitwiseXor

Binary

x := $04 xor $ff

Binary xor operator.

ShiftLeft

Binary

x := 4 shl 1;

shl/shift left operator.

ShiftRight

Binary

x := 4 shr 1;

shr/shift right operator.

Equal

Binary Boolean

Return true if the two input parameters are equal.

NotEqual

Binary Boolean

Return true if the two input parameters are not equal.

Less

Binary Boolean

Returns if the left value is less than the right one.

LessOrEqual

Binary Boolean

Returns if the left value is less than or equal to the right one.

Greater

Binary Boolean

Returns if the left value is greater than the right one.

GreaterOrEqual

Binary Boolean

Returns if the left value is greater than or equal the right one.

In

Binary Boolean

a in [a,b,c]

Returns if the value on the left side is in the value on the right side. For the in keyword in Oxygene.


See Also