TurboScript
TurboScript copied to clipboard
User wants to have method overload
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);
}
}
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);
}
}
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.
Sure, That's better!