Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,30 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable {
case Valid(a) => Right(a)
}

/**
* Converts the value to an EitherT[F, E, A]
*
* Example:
* {{{
* scala> import cats.syntax.all._
* scala> given ExecutionContext = ExecutionContext.global
Copy link
Author

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)

Copy link
Member

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:

Suggested change
* scala> given ExecutionContext = ExecutionContext.global
* scala> import scala.concurrent.ExecutionContext.Implicits.global

*
* scala> val v1 = "error".invalid[Int]
* scala> val v2 = 123.valid[String]
*
* scala> v1.toEitherT[Future].value
* res0: Future[Either[String, Int]] = Future(Left(error))
*
* scala> v2.toEitherT[Future].value
* res1: Future[Either[String, Int]] = Future(Right(123))
* }}}
*/
def toEitherT[F[_]: Applicative]: EitherT[F, E, A] =
this match {
case Invalid(e) => EitherT.leftT(e)
case Valid(a) => EitherT.rightT(a)
}
Comment on lines +287 to +290
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And perhaps

Suggested change
this match {
case Invalid(e) => EitherT.leftT(e)
case Valid(a) => EitherT.rightT(a)
}
EitherT.fromEither(this.toEither)

is cleaner


/**
* Returns Valid values wrapped in Some, and None for Invalid values
*
Expand Down
12 changes: 6 additions & 6 deletions docs/datatypes/validated.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ But, what about if we want _another_ way of combining? We can provide our custom
Cats offers you a nice set of combinators for transforming your `Validated` based approach to an `Either` one and vice-versa.
We've used `.toValidated` in our second example, now let's see how to use `.toEither`.

#### From `Validated` to `Either`
#### From `Validated` to `Either`/`EitherT`

To do this, simply use `.toEither` combinator:
To do this, simply use `.toEither`/`.toEitherT` combinator:

```scala mdoc
// Successful case
Expand All @@ -319,7 +319,7 @@ FormValidatorNec.validateForm(
firstName = "John",
lastName = "Doe",
age = 5
).toEither
Copy link
Member

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?

).toEitherT[Future]
```

With this conversion, as you can see, we got an `Either` with a `NonEmptyChain` detailing the possible validation errors or our `RegistrationData` object.
Expand Down Expand Up @@ -591,7 +591,7 @@ validatedMonad.tuple2(Validated.invalidNec[String, Int]("oops"), Validated.inval
```

This one short circuits! Therefore, if we were to define a `Monad` (or `FlatMap`) instance for `Validated` we would
have to override `ap` to get the behavior we want.
have to override `ap` to get the behavior we want.

```scala mdoc:silent:nest
import cats.Monad
Expand Down Expand Up @@ -629,14 +629,14 @@ But then the behavior of `flatMap` would be inconsistent with that of `ap`, and
```scala
// the `<->` operator means "is equivalent to" and returns a data structure
// `IsEq` that is used to verify the equivalence of the two expressions
def flatMapConsistentApply[A, B](fa: F[A], fab: F[A => B]): IsEq[F[B]] =
def flatMapConsistentApply[A, B](fa: F[A], fab: F[A => B]): IsEq[F[B]] =
fab.ap(fa) <-> fab.flatMap(f => fa.map(f))
```

```scala mdoc:silent
import cats.laws._

val flatMapLawsForAccumulatingValidatedMonad =
val flatMapLawsForAccumulatingValidatedMonad =
FlatMapLaws[Validated[NonEmptyChain[String], *]](accumulatingValidatedMonad)

val fa = Validated.invalidNec[String, Int]("oops")
Expand Down
Loading