Skip to content

Commit

Permalink
Add Either.unit, use in Applicative[Either[A, ?]]
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker authored and travisbrown committed Mar 10, 2020
1 parent fc23079 commit 222bd5a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances {
implicit def catsStdInstancesForEither[A]
: MonadError[Either[A, *], A] with Traverse[Either[A, *]] with Align[Either[A, *]] =
new MonadError[Either[A, *], A] with Traverse[Either[A, *]] with Align[Either[A, *]] {
override def unit: Either[A, Unit] = Either.unit

def pure[B](b: B): Either[A, B] = Right(b)

def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] =
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ final class EitherObjectOps(private val either: Either.type) extends AnyVal { //
case None => left[A, B](ifNone)
case Some(a) => right(a)
}

/** Cached value of `Right(())` to avoid allocations for a common case. */
def unit[A]: Either[A, Unit] = EitherUtil.unit
}

final class LeftOps[A, B](private val left: Left[A, B]) extends AnyVal {
Expand Down Expand Up @@ -456,4 +459,6 @@ private[cats] object EitherUtil {
right.asInstanceOf[Either[C, B]]
def rightCast[A, B, C](left: Left[A, B]): Either[A, C] =
left.asInstanceOf[Either[A, C]]

private[cats] val unit = Right(())
}

0 comments on commit 222bd5a

Please sign in to comment.