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 auto variance as additional import #3436

Merged
merged 4 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ TAGS
.DS_Store
.vscode
.sbtrc
.semanticdb
LukaJCB marked this conversation as resolved.
Show resolved Hide resolved
metals.sbt
*.sublime-project
*.sublime-workspace
tests.iml
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/AutoVariance.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cats

trait AutoVariance {
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)
LukaJCB marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 3 additions & 1 deletion core/src/main/scala/cats/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ object implicits
with instances.AllInstancesBinCompat3
with instances.AllInstancesBinCompat4
with instances.AllInstancesBinCompat5
with instances.AllInstancesBinCompat6
with instances.AllInstancesBinCompat6 {
object variance extends AutoVariance
}
32 changes: 32 additions & 0 deletions tests/src/test/scala/cats/tests/VarianceSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cats.tests

import cats.{Bifunctor, Contravariant, Functor}
import cats.implicits._
import cats.implicits.variance._

class VarianceSuite extends CatsSuite {

sealed trait Foo
case class Bar(x: Int) extends Foo
case object Baz extends Foo

test("Auto-variance should infer subtypes correctly") {
def shouldInfer[F[_]: Functor](fi: F[Int]) =
fi.map(i => if (true) Left(Bar(i)) else Right(Baz))

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

test("Auto-variance should infer supertypes correctly") {
def shouldCompile[F[_]: Contravariant](fi: F[Foo])(f: F[Bar] => Int) =
f(fi)
}

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))

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

}