dry-python / dry-python/returns

Feature Request: Convenience constructor for Result

Aperta
#1,627 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
Python
Stelle
4.4k
Fork
155
Merge medio
2h 27m
PR unite (30g)
22

Descrizione

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!

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.