uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Return a Result<T, E> without throwing an error

Open franklinblanco opened this issue 2 years ago • 4 comments

This is more of a question. Is there any way to convert Result<T, E> into a swift Result? I really REALLY dislike exceptions, rust has taught me better ways. I don't need the error to be generic, ideally I would create a custom result type RResult<T> where the Err variant is always the same. Like this: Screenshot 2022-11-30 at 7 13 06 PM I know this doesn't work but it would basically be this. Screenshot 2022-11-30 at 7 10 55 PM

Thanks in advance for any help!

┆Issue is synchronized with this Jira Task ┆Issue Number: UNIFFI-217

franklinblanco avatar Nov 30 '22 23:11 franklinblanco

There's currently no built-in way to do that. You could have a wrapper on the Swift side that turns the value/exception back into a result.

badboy avatar Dec 01 '22 10:12 badboy

There's currently no built-in way to do that. You could have a wrapper on the Swift side that turns the value/exception back into a result.

Thank you for the help!

franklinblanco avatar Dec 01 '22 14:12 franklinblanco

You should stick with the paradigm of the language that you are using. Swift has first-class support for checked exceptions, and avoiding exceptions is just going to result in sub-par Swift code, simply because alternatives are not as well supported.

arg0d avatar Mar 06 '23 15:03 arg0d

I'm running into the same issue with Kotlin code: people ask me if it's possible to return a Kotlin Result instead of a (possibly checked) exception.

mgeisler avatar Mar 04 '24 15:03 mgeisler