Skip to content

constructors that return result<> #1114

Closed

Description

@OmarTawfik

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..

Activity

  1. alexcrichton commented on Jan 8, 2025

    @alexcrichton
    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.

  2. OmarTawfik commented on Jan 9, 2025

    @OmarTawfik
    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.

  3. alexcrichton commented on Jan 9, 2025

    @alexcrichton
    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!

  4. OmarTawfik commented on Jan 9, 2025

    @OmarTawfik
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions