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

Fix EitherT Bifunctor priority, add Bifoldable instance #4576

Merged
merged 1 commit into from
Mar 23, 2024
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
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