bytecodealliance / wit-bindgen

A language binding generator for WebAssembly interface types
Apache License 2.0
948 stars 184 forks source link

Make C# bindings more ergonomic #964

Closed dicej closed 1 month ago

dicej commented 1 month ago

Currently, the C# generator generates and uses Result and Option types in a pretty literal translation of the WIT input, forcing the application programmer to use e.g. IsOk/AsOk to extract the value, which is awkward. For functions which return result<T, E>, a more ergonomic option would be to generate a method which returns T and throws E (or some exception type with a payload of type E). For option<T> a more idiomatic C# representation would be T?.

We still have to be careful about nested types (e.g. result<result<T, E>, E> and option<option<T>>) to avoid ambiguity, and sometimes result is used as a field or parameter, so we may still need to generate Result and Option types for use in those cases, but in most cases we can do the idiomatic thing. Also, a function returning result<result<T, E>, E> could be translated into a method that returns T and throws WitException<E> with a NestingLevel read-only property to disambiguate which level of err occurred -- all without resorting to use of the Result type.

If there are no concerns, I'm planning to start working on a PR that implements the above.

cc @SteveSandersonMS

yowl commented 1 month ago

Makes sense, thanks

jsturtevant commented 1 month ago

sounds good to me