Skip to content

Commit

Permalink
Fix EitherT Bifunctor priority, add Bifoldable instance
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Mar 15, 2024
1 parent d323fac commit 6f3690d
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 @@ -966,9 +966,13 @@ abstract private[data] class EitherTInstances extends EitherTInstances1 {
Contravariant[Show].contramap(sh)(_.value)

implicit def catsDataBifunctorForEitherT[F[_]](implicit F: Functor[F]): Bifunctor[EitherT[F, *, *]] =
new EitherTBifunctor[F] {
val F0: Functor[F] = 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 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,11 +1097,6 @@ 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 catsDataMonadErrorForEitherT[F[_], L](implicit F0: Monad[F]): MonadError[EitherT[F, L, *], L] =
new EitherTMonadError[F, L] {
implicit val F = F0
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 6f3690d

Please sign in to comment.