This repository was archived by the owner on Mar 4, 2024. It is now read-only.
  
  
  - 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Operators
        Lucas Vos edited this page Jul 16, 2014 
        ·
        4 revisions
      
    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);
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
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
Registering an Enumaration to the system is done by typing System.Type.registerEnum and the enumaration and after the comma you type the string name.
Sample
module System
{
    export enum SampleColor
    {
        Red,
        White,
    } 
    System.Type.registerEnum(SampleColor, "System.SampleColor");
}