-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added liftTo to Try Either and Option (#2179)
- Loading branch information
1 parent
60c3546
commit 9135035
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cats | ||
package syntax | ||
|
||
import scala.util.Try | ||
|
||
trait TrySyntax { | ||
implicit final def catsSyntaxTry[A](t: Try[A]): TryOps[A] = new TryOps(t) | ||
} | ||
|
||
|
||
final class TryOps[A](val self: Try[A]) extends AnyVal { | ||
|
||
/** | ||
* lift the `try` into a `F[_]` with `ApplicativeError[F, Throwable]` instance | ||
* | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> import util.Try | ||
* | ||
* scala> val s: Try[Int] = Try(3) | ||
* scala> s.liftTo[Either[Throwable, ?]] | ||
* res0: Either[Throwable, Int] = Right(3) | ||
* | ||
* scala> val f: Try[Int] = Try(throw new Throwable("boo")) | ||
* scala> f.liftTo[Either[Throwable, ?]] | ||
* res0: Either[Throwable, Int] = Left(java.lang.Throwable: boo) | ||
* }}} | ||
*/ | ||
def liftTo[F[_]](implicit F: ApplicativeError[F, Throwable]): F[A] = | ||
F.fromTry(self) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters