-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about naming this catchNonFatal
to match the method on Xor
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/shuold/should
👍 |
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this just delegate to catchNonFatal
with a.value
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
MonadError
cases.It is somewhat limited due to requiring
Throwable
, we could put it on theMonadError
object rather than instance, and we could introduce a new typeclassCatchable[M]
?I think this covers 90% of the cases without adding too much complexity.