-
-
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 MonadError.ensure method #1013
Conversation
trait MonadErrorSyntax { | ||
|
||
implicit def monadErrorSyntax[F[_], E, A](fa: F[A])(implicit F: MonadError[F, E]): MonadErrorOps[F, E, A] = | ||
new MonadErrorOps(fa) |
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 noticed that the applicativeErrorSyntax
method was defined for a F[_, _]
(notice the kind), but I have type inference issues with it. So here I defined it for a F[_]
. Am I wrong?
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.
No, I think that you are right. Coincidentally this just came up in a gitter conversation.
Thanks, @julienrf! I like this.
Yeah, I think you are right.
That's a good question. I think I might have gone overboard with laws on This method is equivalent to |
Oh, yes, I will ensure that. (I didn’t notice this method on |
d4c0310
to
05bf655
Compare
👍 lgtm Thanks @julienrf! |
Actually there is a problem: the tests don’t test the Maybe I should roll my own Xor-like type, just for the tests… |
@julienrf ah good catch. There's a |
Hi, I changed the tests to use |
Current coverage is 88.51%
@@ master #1013 diff @@
==========================================
Files 214 216 +2
Lines 2719 2724 +5
Methods 2655 2662 +7
Messages 0 0
Branches 59 57 -2
==========================================
+ Hits 2407 2411 +4
- Misses 312 313 +1
Partials 0 0
|
I don't understand why this is reporting a code coverage drop - it doesn't seem to me like it should result in one. 👍 thanks! |
This is a small thing, but do you think it would be possible to reverse the order or the error and the predicate? I feel like |
@non I think that elsewhere (with |
Fair enough. For whatever reason I read the call as "ensuring " and so seeing the error first confused me. I wonder if something like this would be complimentary? def errorIf(fa: F[A])(f: A => Option[E]): F[A] =
flatMap(fa)(a => f(a) match {
case None => pure(a)
case Some(e) => raiseError(e)
}) |
I often have a predicate longer than the error value so for me it often reads better to have the error value first.
|
Sure! 👍 |
This adds an
ensure
method toMonadError
.I have a couple of questions:
ApplicativeError
, is it?ensure(fa)(_ => true)(error) <-> fa