Skip to content

Commit

Permalink
Merge pull request #4576 from joroKr21/bugfix/eithert
Browse files Browse the repository at this point in the history
Fix EitherT Bifunctor priority, add Bifoldable instance
  • Loading branch information
danicheg authored Mar 23, 2024
2 parents 236b7fd + 04f7c28 commit 3d3f63a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
15 changes: 7 additions & 8 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,8 @@ abstract private[data] class EitherTInstances extends EitherTInstances1 {
implicit def catsDataShowForEitherT[F[_], L, R](implicit sh: Show[F[Either[L, R]]]): Show[EitherT[F, L, R]] =
Contravariant[Show].contramap(sh)(_.value)

implicit def catsDataBifunctorForEitherT[F[_]](implicit F: Functor[F]): Bifunctor[EitherT[F, *, *]] =
new EitherTBifunctor[F] {
val F0: Functor[F] = F
}
implicit def catsDataBitraverseForEitherT[F[_]](implicit F: Traverse[F]): Bitraverse[EitherT[F, *, *]] =
new EitherTBitraverse[F] { val F0 = F }

implicit def catsDataTraverseForEitherT[F[_], L](implicit FF: Traverse[F]): Traverse[EitherT[F, L, *]] =
new EitherTTraverse[F, L] with EitherTFunctor[F, L] {
Expand Down Expand Up @@ -1093,10 +1091,11 @@ abstract private[data] class EitherTInstances1 extends EitherTInstances2 {
val F0: PartialOrder[F[Either[L, R]]] = F
}

implicit def catsDataBitraverseForEitherT[F[_]](implicit F: Traverse[F]): Bitraverse[EitherT[F, *, *]] =
new EitherTBitraverse[F] with EitherTBifunctor[F] {
val F0: Traverse[F] = F
}
implicit def catsDataBifunctorForEitherT[F[_]](implicit F: Functor[F]): Bifunctor[EitherT[F, *, *]] =
new EitherTBifunctor[F] { val F0 = F }

implicit def catsDataBifoldableForEitherT[F[_]](implicit F: Foldable[F]): Bifoldable[EitherT[F, *, *]] =
new EitherTBifoldable[F] { val F0 = F }

implicit def catsDataMonadErrorForEitherT[F[_], L](implicit F0: Monad[F]): MonadError[EitherT[F, L, *], L] =
new EitherTMonadError[F, L] {
Expand Down
21 changes: 20 additions & 1 deletion tests/shared/src/test/scala/cats/tests/EitherTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class EitherTSuite extends CatsSuite {
implicit val iso: Isomorphisms[EitherT[ListWrapper, String, *]] = Isomorphisms
.invariant[EitherT[ListWrapper, String, *]](EitherT.catsDataFunctorForEitherT(ListWrapper.functor))

// Test instance summoning
def summon[F[_]: Traverse](): Unit = {
Bifunctor[EitherT[F, *, *]]
Bifoldable[EitherT[F, *, *]]
}

checkAll("EitherT[Eval, String, *]", DeferTests[EitherT[Eval, String, *]].defer[Int])

{
Expand Down Expand Up @@ -74,6 +80,20 @@ class EitherTSuite extends CatsSuite {
)
}

{
// if a Foldable for F is defined
implicit val F: Foldable[ListWrapper] = ListWrapper.foldable

checkAll("EitherT[ListWrapper, Int, *]", FoldableTests[EitherT[ListWrapper, Int, *]].foldable[Int, Int])
checkAll("Foldable[EitherT[ListWrapper, Int, *]]",
SerializableTests.serializable(Foldable[EitherT[ListWrapper, Int, *]])
)
checkAll("EitherT[ListWrapper, *, *]", BifoldableTests[EitherT[ListWrapper, *, *]].bifoldable[Int, Int, Int])
checkAll("Bifoldable[EitherT[ListWrapper, *, *]]",
SerializableTests.serializable(Bifoldable[EitherT[ListWrapper, *, *]])
)
}

{
// if a Traverse for F is defined
implicit val F: Traverse[ListWrapper] = ListWrapper.traverse
Expand All @@ -90,7 +110,6 @@ class EitherTSuite extends CatsSuite {
checkAll("Bitraverse[EitherT[ListWrapper, *, *]]",
SerializableTests.serializable(Bitraverse[EitherT[ListWrapper, *, *]])
)

}

{
Expand Down

0 comments on commit 3d3f63a

Please sign in to comment.