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

constructors that return result<> #1114

Closed
OmarTawfik opened this issue Jan 7, 2025 · 4 comments
Closed

constructors that return result<> #1114

OmarTawfik opened this issue Jan 7, 2025 · 4 comments

Comments

@OmarTawfik
Copy link

Today, the following syntax is illegal:

resource blob {
    constructor(data: list<u8>) -> result<blob, error-type>;
}

Since constructors have no return types. However, this is a perfectly valid use case for languages like TypeScript, as result<> return type are lowered into blob directly, using exceptions to propagate error-type values if they occur. Without it, APIs are forced to use alternative method names like blob::create(), which is not standard, and hurts readability/discoverability.

Thoughts? I wonder if it is possible to enable this use case..

@alexcrichton
Copy link
Member

Thanks for the report! This might be best discussed upstream since that's what governs the design of WIT itself rather than this repository, but in the meantime the main rationale for this is that many languages with native constructor support (e.g. JS and C++) can't support this which is why it's not supported. The alternative is a static function which returns the result, and that wouldn't get constructor treatment.

@OmarTawfik
Copy link
Author

I will close and re-open upstream. Thanks!

but in the meantime the main rationale for this is that many languages with native constructor support (e.g. JS and C++) can't support this which is why it's not supported.

I think that is orthogonal since neither JS jor C++ support result<> in normal functions either way. They emulate this via exceptions. My suggestion is to do the same for constructors, rather than special casing them.

@alexcrichton
Copy link
Member

Oh dear, sorry I realize I didn't fully read over your initial posting here which already explained everything I said. Sorry about that!

@OmarTawfik
Copy link
Author

No worries at all :) thanks for considering!

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

No branches or pull requests

2 participants