TurboScript icon indicating copy to clipboard operation
TurboScript copied to clipboard

User wants to have method overload

Open nidin opened this issue 7 years ago • 3 comments

What? User want to have method overload, exclude constructor from this scope.

How?

class Foo {
    value:float32;
    constructor() {
    }
    add(v:float32):void {
        this.value = this.value + v;
    }
    add(v:int32):void {
        this.value = this.value + (v as float32);
    }
}

nidin avatar Mar 25 '17 10:03 nidin

How about declare overloads as 'union types' for example:

class Foo {
      static add(a: float32|float64|int32, b: float32|float64|int32): float64 {
           return a + b;
      }
}

Is actually compiles to:

class Foo {
      static add(a: float32, b: float32): float64 {
          return <float64>(a + b);
      }

      static add(a: float64, b: float64): float64 {
          return a + b;
      }

      static add(a: int32, b: int32): float64 {
          return <float64>(a + b);
      }
}

MaxGraey avatar Mar 28 '17 11:03 MaxGraey

But user can pass any mixed combination of union types that might break the code. for example.

let a:int32 = 10;
let b:float32 = 1.2356;
Foo.add(a, b); //error in wasm

better idea is

class Foo {
      static add<T>(a: T, b: T): float64 {
           return <float64>(a + b);
      }
}

in this way a and b are of same type. this can handle all cases including classes with + operator but classes cannot cast to float64.

nidin avatar Mar 28 '17 13:03 nidin

Sure, That's better!

MaxGraey avatar Mar 28 '17 13:03 MaxGraey