dry-python / dry-python/returns
Feature Request: Convenience constructor for Result
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 155
- Avg merge
- 2h 27m
- Merged PRs (30d)
- 22
Description
Hello! Apologies for filing as a "bug" but didn't see another issue type I could file as.
I was wondering if there was any appetite for an additional constructor on `Result` that would implement something akin to [Scala's Try constructor](https://www.scala-lang.org/api/2.13.6/scala/util/Try.html) (e.g. https://alvinalexander.com/scala/try-success-failure-example-reading-file/)
As a very primitive example, I often use a utility function that looks something like this:
```
def res_lift(lambda_function: Callable[[], T]) -> Result[T, Exception]:
try:
return Result.from_value(lambda_function())
except Exception as e:
return Result.from_failure(e)
```
So that I can do something like `res_lift(lambda: something_that_might_error())` and easily get a ResultE out. This would kind of be like Maybe's `.from_optional` method (https://returns.readthedocs.io/en/latest/pages/maybe.html#maybe-container), except it would be for ResultE.
I'm not totally sure it's feasible to implement this in a way that's general for all Result Failure types (I recognize that Exception isn't the only Failure type that people use), but if there's interest, I'd be happy to try and put together a PR for this or collaborate however would be useful!
Contributor guide
Assessment
This issue has not been assessed yet.