Stream: wit-bindgen

Topic: Wit resource constructor errors


view this post on Zulip Ari Seyhun (Dec 01 2023 at 06:55):

It seems like resources defined in wit files cannot return an error, or any value?
I tried:

constructor(id: string) -> result<_, string>

However it isn't valid syntax since it wants an ; immediately after the ).

I'm assuming this was initially intentional, however is there a good reason for having this restriction where constructors cannot fail?
I'm not sure how to model it so that it can fail, unless I did something like:

resource agg {
    constructor();
    init(id: string) -> result<_, string>;
}

But the rust code would just be Default::default() for the constructor, and the init would set the ID string?
It's a little messy and it would be much more convenient if I could just return the error directly from the constructor.

view this post on Zulip Till Schneidereit (Dec 01 2023 at 14:17):

not all languages support fallible constructors, and we want to provide the ability to create good bindings for all languages, including those.

What you can do instead is add a static function that can internally call the constructor, but returns a result:

resource agg {
    constructor();
    from_id: static func(id: string) -> result<agg, string>;
}

view this post on Zulip Lann Martin (Dec 01 2023 at 14:41):

Note that if you only want a resource to be produced via the static function you shouldn't define constructor(); at all; it is only for exporting infallible constructors.

view this post on Zulip Ari Seyhun (Dec 01 2023 at 14:42):

ah okay, that solves it entirely I guess! Thanks a lot


Last updated: Nov 22 2024 at 17:03 UTC