You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
deffoo(a, b, c):
a=_safe(a)
rval=a(b, c)
ifrval.is_ok:
returnrvalifrval.unwrap_err() isZeroDivisionError:
returnResult.Err("cannot divide by zero")
else:
returnResult.Err("what?")
This works very well for languages that do not support try/except for control flows, which is something that can be mapped to non-Turing complete language subsets such as Starlark.
Thoughts?
The text was updated successfully, but these errors were encountered:
The code sample you provided is a little confusing to read, but are you suggesting that there should be a decorator that converts exceptional code into one that returns Result?
Love this idea, safe would be very cool, but the return type would be Result[T, Exception], which is not the finest grain for the error type.
I ended up having something similar, which I call handle:
E=TypeVar('E', bound=BaseException)
T=TypeVar('T')
defhandle(err_sumtype: type[E], fn: Callable[[], T]) ->Result[T, E]:
""" Encapsulates a function call that expects to return `T` but may fail with exceptions `err_sumtype` (`E`) to return an `option.Result` """try:
returnResult.Ok(fn())
excepterr_sumtypease:
returnResult.Err(e)
Hi there - I love this little library, but I think some utility functions would really be beneficial for migrating exception code to using results.
Some thoughts, similar to the
dry-returns
library, we should introduce asafe
function for a following use case:Imagine refactoring this piece of (very hypothetical code):
It can be very easily refactored with the introduction of
safe
function:then refactoring
foo
becomes:This works very well for languages that do not support try/except for control flows, which is something that can be mapped to non-Turing complete language subsets such as Starlark.
Thoughts?
The text was updated successfully, but these errors were encountered: