-
-
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
feat: add .toEitherT
to Validated
#4554
Conversation
* Example: | ||
* {{{ | ||
* scala> import cats.syntax.all._ | ||
* scala> given ExecutionContext = ExecutionContext.global |
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 believe this is Scala 3 syntax, please replace it with the Scala 2 equivalent (which I assume doc comments are written in)
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.
Didn't compile it myself, but I think this would do it:
* scala> given ExecutionContext = ExecutionContext.global | |
* scala> import scala.concurrent.ExecutionContext.Implicits.global |
Hm, the errors correctly mark:
yet with an extension, this variance difference is no issue: extension [E, A](validated: Validated[E, A])
def toEitherT[F[_]: Applicative]: EitherT[F, E, A] = validated match
case Validated.Invalid(e) => EitherT.leftT(e)
case Validated.Valid(a) => EitherT.rightT(a) I have unfortunately not the slightest idea how to fix this |
this match { | ||
case Invalid(e) => EitherT.leftT(e) | ||
case Valid(a) => EitherT.rightT(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.
And perhaps
this match { | |
case Invalid(e) => EitherT.leftT(e) | |
case Valid(a) => EitherT.rightT(a) | |
} | |
EitherT.fromEither(this.toEither) |
is cleaner
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 for the contribution! The compilation error can be fixed with:
def toEitherT[F[_]: Applicative, EE >: E, AA >: A]: EitherT[F, EE, AA] =
... but that's not what we want, because then .toEitherT
needs three parameters, and it loses its convenience. I thought Ior.toIorT
might serve as prior art, but it doesn't exist. But IorT.fromIor
does! Maybe what we want is EitherT.fromValidated
?
* Example: | ||
* {{{ | ||
* scala> import cats.syntax.all._ | ||
* scala> given ExecutionContext = ExecutionContext.global |
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.
Didn't compile it myself, but I think this would do it:
* scala> given ExecutionContext = ExecutionContext.global | |
* scala> import scala.concurrent.ExecutionContext.Implicits.global |
@@ -319,7 +319,7 @@ FormValidatorNec.validateForm( | |||
firstName = "John", | |||
lastName = "Doe", | |||
age = 5 | |||
).toEither |
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.
The next sentence says we have an Either
. Perhaps it's better to make the EitherT
a separate example?
Agree. Adding a new constructor to Another thought is that |
Thanks for outlining the reasoning, I agree. |
Similar to
.toEither
but withEitherT