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.
Currently, the C# generator generates and uses
Result
andOption
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 returnresult<T, E>
, a more ergonomic option would be to generate a method which returnsT
and throwsE
(or some exception type with a payload of typeE
). Foroption<T>
a more idiomatic C# representation would beT?
.We still have to be careful about nested types (e.g.
result<result<T, E>, E>
andoption<option<T>>
) to avoid ambiguity, and sometimesresult
is used as a field or parameter, so we may still need to generateResult
andOption
types for use in those cases, but in most cases we can do the idiomatic thing. Also, a function returningresult<result<T, E>, E>
could be translated into a method that returnsT
and throwsWitException<E>
with aNestingLevel
read-only property to disambiguate which level oferr
occurred -- all without resorting to use of theResult
type.If there are no concerns, I'm planning to start working on a PR that implements the above.
cc @SteveSandersonMS