Skip to content
Open
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
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/mtl/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ trait AllSyntax
with TellSyntax
with HandleSyntax
with ChronicleSyntax
with OptionSyntax
with EitherSyntax
64 changes: 64 additions & 0 deletions core/src/main/scala/cats/mtl/syntax/either.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats
package mtl
package syntax

object either extends EitherSyntax

private[mtl] trait EitherSyntax {
implicit def catsMtsSyntaxToEitherOps[A, B](oa: Either[A, B]): EitherOps[A, B] =
new EitherOps[A, B](oa)
}

final class EitherOps[A, B] private[mtl] (private val self: Either[A, B]) extends AnyVal {

/**
* Lifts `Either[A, B]` to `F[B]` as long as there's `Raise[F, A]` in the scope and `F` is an
* `Applicative`.
*
* @note
* method `.rescue` in the example requires `ApplicativeError[F, E]` instance.
*
* @example
* (Scala 3)
* {{{
* import scala.util.*
* import cats.mtl.*, syntax.either.*
*
* case class MyErr(err: String) extends Exception(err)
*
* val res1 =
* Handle.allow:
* Right[String, Int](123).liftTo[Try]
* .rescue: err =>
* Failure(MyErr(err))
*
* assertEquals(res1, Success(123))
*
* val res2 =
* Handle.allow:
* Left[String, Int]("OOPS").liftTo[Try]
* .rescue: err =>
* Failure(MyErr(err))
*
* assertEquals(res2, Failure("OOPS"))
* }}}
*/
def liftTo[F[_]](implicit F: Applicative[F], raise: Raise[F, A]): F[B] =
raise.fromEither(self)
}
106 changes: 106 additions & 0 deletions core/src/main/scala/cats/mtl/syntax/option.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats
package mtl
package syntax

object option extends OptionSyntax

private[mtl] trait OptionSyntax {
implicit def catsMtsSyntaxToOptionOps[A](oa: Option[A]): OptionOps[A] = new OptionOps[A](oa)
}

final class OptionOps[A] private[mtl] (private val self: Option[A]) extends AnyVal {
import OptionOps.*

/**
* Lifts `Option[A]` to `F[A]` as long as there's `Raise[F, E]` in the scope and `F` is an
* `Applicative`, with `E` used as the error value when the `Option` is empty.
*
* @note
* method `.rescue` in the example requires `ApplicativeError[F, E]` instance.
*
* @example
* (Scala 3)
* {{{
* import scala.util.*
* import cats.mtl.*, syntax.option.*
*
* case class MyErr(err: String) extends Exception(err)
*
* val res1 =
* Handle.allow:
* Some(123).liftTo[Try]("OOPS")
* .rescue: err =>
* Failure(MyErr(err))
*
* assertEquals(res1, Success(123))
*
* val res2 =
* Handle.allow:
* None.liftTo[Try]("OOPS")
* .rescue: err =>
* Failure(MyErr(err))
*
* assertEquals(res2, Failure(MyErr("OOPS")))
* }}}
*/
def liftTo[F[_]]: LiftToPartiallyApplied[F, A] =
new LiftToPartiallyApplied(self)

/**
* Raises `Option[A]` to `F[Unit]` as an error when present as long as there's `Raise[F, A]`
* in the scope and `F` is an `Applicative`
*
* @note
* method `.rescue` in the example requires `ApplicativeError[F, E]` instance.
*
* @example
* (Scala 3)
* {{{
* import scala.util.*
* import cats.mtl.*, syntax.option.*
*
* case class MyErr(err: String) extends Exception(err)
*
* val res1 =
* Handle.allow:
* Some("OOPS").raiseTo[Try]
* .rescue: err =>
* Failure(MyErr(err))
*
* assertEquals(res1, Failure(MyErr("OOPS")))
*
* val res2 =
* Handle.allow:
* (None: Option[String]).raiseTo[Try]
* .rescue: err =>
* Failure(MyErr(err))
*
* assertEquals(res2, Success(()))
* }}}
*/
def raiseTo[F[_]](implicit F: Applicative[F], raise: Raise[F, A]): F[Unit] =
self.fold(F.unit)(raise.raise)
}

object OptionOps {
final class LiftToPartiallyApplied[F[_], A](private val self: Option[A]) extends AnyVal {
def apply[E](ifEmpty: => E)(implicit F: Applicative[F], raise: Raise[F, E]): F[A] =
raise.fromOption(self)(ifEmpty)
}
}
18 changes: 18 additions & 0 deletions tests/shared/src/test/scala/cats/mtl/tests/Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ final class Syntax extends BaseSuite {
bar[F] *> Bar(42).raise

val _ = foo[Either[Foo, *]]

def fooWithApplicativeError[F[_]](implicit
// `Applicative` is required by `liftTo`.
// `ApplicativeError` is used to enforce checks for non-ambiguity.
F: ApplicativeError[F, Foo],
raise: Raise[F, Foo]): Unit = {

val _ = (
// cats.mtl.syntax.option.*
Some("abc").liftTo[F](Bar(123)): F[String],
Some(Bar(456)).raiseTo[F]: F[Unit],
// cats.mtl.syntax.either.*
Left[Bar, String](Bar(789)).liftTo[F]: F[String],
Right[Bar, String]("def").liftTo[F]: F[String]
)
}

fooWithApplicativeError[Either[Foo, *]]
}

test("Handle") {
Expand Down