Skip to content
This repository has been archived by the owner on Feb 28, 2018. It is now read-only.

User wants to have method overload #15

Open
nidin opened this issue Mar 25, 2017 · 3 comments
Open

User wants to have method overload #15

nidin opened this issue Mar 25, 2017 · 3 comments

Comments

@nidin
Copy link
Contributor

nidin commented Mar 25, 2017

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);
    }
}
@MaxGraey
Copy link
Contributor

MaxGraey commented Mar 28, 2017

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);
      }
}

@nidin
Copy link
Contributor Author

nidin commented Mar 28, 2017

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.

@MaxGraey
Copy link
Contributor

Sure, That's better!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants