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

Add Either.unit, use in Applicative[Either[A, ?]] #3346

Merged
merged 1 commit into from
Mar 10, 2020
Merged
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/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(())
}