Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Operators

Lucas Vos edited this page Jul 16, 2014 · 4 revisions

Operator overloading

in C# operators can be overloaded, which is not supported in Typescript. To mimic this behaviour, we use a convention based approach, along with some helper methods.

Resulting in this kind of coding C# sample

var P1 = new Point(1,1);
var P2 = new Point(3,3);
var P = P1 + P2;

In Typescript

var P1 = new Point(1,1);
var P2 = new Point(3,3);
var P = Ops.Add(P1 , P2);

Example

An operator overload in C#

public static Complex operator +(Complex c1, Complex c2) 
  {
     return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
  }

An implementation in Typescript

public static op_Addition(c1 : Complex, c2: Complex) : Complex
  {
     return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
  }

There are a couple of operator groups

  • Unary operators
  • Binary operators
  • Explicit
  • Implicit

op_Incerement op_Decrement

Binary operators

Use the following naming conventions to

+, -, *, /, %, &, |, ^, <<, >>

  • op_Addition
  • op_Substraction
  • op_Multiplication
  • op_Division
  • op_Modulus
  • op_GreaterThan
  • op_GreaterThanOrEqual
  • op_LessThan
  • op_LessThenOrEqual
  • op_And
  • op_Or
  • op_Xor
  • op_RightShift
  • op_LeftShift
Clone this wiki locally