diff --git a/core/src/main/scala/cats/Eval.scala b/core/src/main/scala/cats/Eval.scala index 28cd4f1fee..cf5a8f75f4 100644 --- a/core/src/main/scala/cats/Eval.scala +++ b/core/src/main/scala/cats/Eval.scala @@ -416,6 +416,22 @@ private[cats] sealed abstract class EvalInstances extends EvalInstances0 { implicit def catsGroupForEval[A: Group]: Group[Eval[A]] = new EvalGroup[A] { val algebra: Group[A] = Group[A] } + + implicit val catsRepresentableForEval: Representable.Aux[Eval, Unit] = new Representable[Eval] { + override type Representation = Unit + + override val F: Functor[Eval] = Functor[Eval] + + /** + * Create a function that "indexes" into the `F` structure using `Representation` + */ + override def index[A](f: Eval[A]): Unit => A = (_: Unit) => f.value + + /** + * Reconstructs the `F` structure using the index function + */ + override def tabulate[A](f: Unit => A): Eval[A] = Eval.later(f(())) + } } private[cats] sealed abstract class EvalInstances0 extends EvalInstances1 { diff --git a/tests/src/test/scala/cats/tests/RepresentableSuite.scala b/tests/src/test/scala/cats/tests/RepresentableSuite.scala index e4186bd827..2455d5fd4e 100644 --- a/tests/src/test/scala/cats/tests/RepresentableSuite.scala +++ b/tests/src/test/scala/cats/tests/RepresentableSuite.scala @@ -4,7 +4,7 @@ import cats.laws.discipline.SemigroupalTests.Isomorphisms import cats.laws.discipline.arbitrary._ import cats.laws.discipline.eq._ import cats.laws.discipline.{BimonadTests, MonadTests, RepresentableTests, SerializableTests} -import cats.{Bimonad, Eq, Id, Representable} +import cats.{Bimonad, Eq, Eval, Id, Representable} import org.scalacheck.Arbitrary import cats.data.Kleisli @@ -21,6 +21,8 @@ class RepresentableSuite extends CatsSuite { checkAll("Pair[String, String] <-> Boolean => String", RepresentableTests[Pair, Boolean].representable[String]) checkAll("Representable[Pair]", SerializableTests.serializable(Representable[Pair])) + checkAll("Eval[Int] <-> Unit => Int", RepresentableTests[Eval, Unit].representable[Int]) + checkAll("Representable[Eval]", SerializableTests.serializable(Representable[Eval])) { implicit val representableKleisliPair = Kleisli.catsDataRepresentableForKleisli[Pair, Boolean, String]