Skip to content

Commit

Permalink
Full Bifunctor widening
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaJCB committed May 27, 2020
1 parent d546614 commit 974a335
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions core/src/main/scala/cats/AutoVariance.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cats

trait AutoVariance {
trait AutoVariance extends AutoVarianceLowPriority {
implicit def autoWidenFunctor[F[_]: Functor, A, B >: A](fa: F[A]): F[B] = Functor[F].widen(fa)

implicit def autoNarrowContravariant[F[_]: Contravariant, A, B <: A](fa: F[A]): F[B] = Contravariant[F].narrow(fa)

implicit def autoLeftWidenBifunctor[F[_, _]: Bifunctor, A, B >: A, C](fac: F[A, C]): F[B, C] =
Bifunctor[F].leftWiden(fac)
}

trait AutoVarianceLowPriority {
implicit def autoWidenBifunctor[F[_, _]: Bifunctor, A, B >: A, C, D >: C](fac: F[A, C]): F[B, D] =
Bifunctor[F].leftWiden(Bifunctor[F].rightFunctor.widen(fac))
}
4 changes: 2 additions & 2 deletions tests/src/test/scala/cats/tests/VarianceSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class VarianceSuite extends CatsSuite {

test("Auto-variance should left widen a bifunctor automatically") {
def shouldInfer[F[_, _]: Bifunctor](fi: F[Int, Int]) =
fi.leftMap(i => if (true) Left(Bar(i)) else Right(Baz))
fi.bimap(i => if (true) Left(Bar(i)) else Right(Baz), i => if (true) Left(Bar(i)) else Right(Baz))

def inferred[F[_, _]: Bifunctor](fi: F[Int, Int]): F[Either[Foo, Foo], Int] = shouldInfer[F](fi)
def inferred[F[_, _]: Bifunctor](fi: F[Int, Int]): F[Either[Foo, Foo], Either[Foo, Foo]] = shouldInfer[F](fi)
}

}

0 comments on commit 974a335

Please sign in to comment.