dry-python / dry-python/returns

Feature Request: Convenience constructor for Result

Đang mở
#1,627 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
4.4k
Fork
155
Merge trung bình
2 giờ 27 phút
Pull request đã merge (30 ngày)
22

Mô tả

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!

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.