I have an F[_] : cats.Applicative
. I want to convert Try[T]
to F[T]
.
val a: Try[T] = ???
val b: F[T] = a match {
case scala.util.Success(result) => implicitely[Applicative[F]].pure(result)
case scala.util.Failure(throwable) => ??? // what should go here?
You need an ApplicativeError[F, Throwable]
instead of just an Applicative
Then you can just:
val b = ApplicativeError[F, Throwable].fromTry(a)
2 Likes
Can also use .liftTo
syntax.
@ Try(1).liftTo[IO]
res9: IO[Int] = Pure(1)
@ Try(???).liftTo[IO]
res10: IO[Nothing] = RaiseError(scala.NotImplementedError: an implementation is missing)
@ Try(1).liftTo[Either[Throwable,*]]
res11: Either[Throwable, Int] = Right(1)
@ Try(???).liftTo[Either[Throwable,*]]
res12: Either[Throwable, A] = Left(scala.NotImplementedError: an implementation is missing)
2 Likes