add catchNonFatal to MonadError#1269
Conversation
Current coverage is 90.57% (diff: 100%)@@ master #1269 diff @@
==========================================
Files 243 243
Lines 3286 3301 +15
Methods 3234 3243 +9
Messages 0 0
Branches 49 56 +7
==========================================
+ Hits 2973 2990 +17
+ Misses 313 311 -2
Partials 0 0
|
| * Often E is Throwable. Here we try to call pure or catch | ||
| * and raise. | ||
| */ | ||
| def tryCatch[A](a: => A)(implicit ev: Throwable =:= E): F[A] = |
There was a problem hiding this comment.
What do you think about naming this catchNonFatal to match the method on Xor?
There was a problem hiding this comment.
good call. Updated.
| forAll { e: Either[String, Int] => | ||
| val str = e.fold(identity, _.toString) | ||
| val res = MonadError[Try, Throwable].catchNonFatal(str.toInt) | ||
| // the above shuold just never cause an uncaught exception |
|
👍 |
| * Often E is Throwable. Here we try to call pure or catch | ||
| * and raise. | ||
| */ | ||
| def catchNonFatal[A](a: => A)(implicit ev: Throwable =:= E): F[A] = |
There was a problem hiding this comment.
My first inclination would be to make this eq: Throwable <:< E—that will work in all reasonable places where E is statically known, and it eliminates the need for guesswork about whether to write E =:= Throwable (which won't work) or Throwable =:= E (which will) for people who want to support these methods in generic contexts.
|
👍 but see my question above. |
|
this has two 👍 is it okay to merge one's own PR? |
| * Often E is Throwable. Here we try to call pure or catch | ||
| * and raise | ||
| */ | ||
| def catchNonFatalEval[A](a: Eval[A])(implicit ev: Throwable <:< E): F[A] = |
There was a problem hiding this comment.
Could this just delegate to catchNonFatal with a.value ?
There was a problem hiding this comment.
I think that would be slightly slower since you would allocate another closure to do the call by name.
|
I've done it before, go ahead and merge :) |
This method seems really convenient for many common
MonadErrorcases.It is somewhat limited due to requiring
Throwable, we could put it on theMonadErrorobject rather than instance, and we could introduce a new typeclassCatchable[M]?I think this covers 90% of the cases without adding too much complexity.