-
-
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
Improve MonadError rethrow syntax to be more flexible #2880
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2880 +/- ##
=======================================
Coverage 94.21% 94.21%
=======================================
Files 368 368
Lines 6948 6948
Branches 308 307 -1
=======================================
Hits 6546 6546
Misses 402 402
Continue to review full report at Codecov.
|
Hi @bpholt thanks for creating this PR, is there a reason you're specializing it to |
agree with @LukaJCB final class MonadErrorRethrowOps[F[_], E, A](private val fea: F[Either[E, A]]) extends AnyVal {
def rethrow(implicit F: MonadError[F, E]): F[A] = F.rethrow(fea)
} to def rethrow(implicit F: MonadError[F, _ :> E]): F[A] = F.rethrow(fea) will achieve what you want? |
@LukaJCB Widening beyond @kailuowang That doesn't look like it compiles (I assume you meant
I tried implementing this as a low-priority implicit (to avoid ambiguity with the existing one, ignoring bincompat for now) but I can't seem to get the type inference to work. implicit final def catsSyntaxMonadErrorWidenRethrow[F[_], E, EE >: E, A](
fea: F[Either[E, A]]
)(implicit F: MonadError[F, EE]): MonadErrorWidenRethrowOps[F, E, EE, A] =
new MonadErrorWidenRethrowOps(fea) If I specify at the call site the super type |
@bpholt thanks for trying this out. As a work around I suggested that we make two changes (both binary compatible), I tested they should work. def rethrow[A, EE <: E](fa: F[Either[EE, A]]): F[A] =
flatMap(fa)(_.fold(raiseError, pure)) Change 2, in MonadError syntax we change the exising def rethrow(implicit F: MonadError[F, _ >: E]): F[A] =
F.flatMap(fea)(_.fold(F.raiseError, F.pure)) //it's a dup from the type class implemenation, due to https://github.com/scala/bug/issues/11562. Once fixed should be able to replace with `F.rethrow(fea)` |
fixes #2561 cc @djspiewak |
instance for a supertype of the error type
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.
Thanks!
Update: clicked the wrong button
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.
Looks good, thanks! :)
This allows calling
MonadError
'srethrow
directly on anF[Either[E, A]]
, whereE <: Throwable
. Currently I don't think this is possible: one has to explicitlyleftWiden
toThrowable
.