Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to call a static function on generic type <T>? #542

Closed
evgenykuzyakov opened this issue Mar 15, 2019 · 3 comments
Closed
Labels

Comments

@evgenykuzyakov
Copy link

evgenykuzyakov commented Mar 15, 2019

Example:

class Temp {
  static decode(buf: Uint8Array): Temp {
    return new Temp(); 
  }
}

function decode<T>(buf: Uint8Array): T {
  return T.decode(buf);
}

let a = decode<Temp>(new Uint8Array());
@MaxGraey
Copy link
Member

MaxGraey commented Mar 15, 2019

No, this also not possible in typescript.
But you could do something like this:

class Base {
  static decode<T>(buf: Uint8Array): T {
    return instantiate<T>(); // `instantiate` is AssemblyScript's builtin
  }
}

class Temp extends Base {
}

let a = Base.decode<Temp>(new Uint8Array(1));

@jtenner
Copy link
Contributor

jtenner commented Mar 15, 2019

If you know the output is a reference, you can use the changetype macro like this.

changetype<T>(changetype<usize>(T.decode(buf)));

You will be responsible for making sure the output of the decode function matches the definition of the T data type.

This is not safe, and you would need to know what you are doing.

@evgenykuzyakov
Copy link
Author

Thanks

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

No branches or pull requests

3 participants