找不到Tuple2K的仿函数实例

时间:2018-05-17 07:05:19

标签: scala functor scala-cats comonad

我有玩具DSL

case class Logging[A](msg: String, action: A)
case class Persist[A](msg: String, action: A)
type Effect[A] = EitherK[Logging, Persist, A]

我想与同样的玩具翻译配对

case class CoLogging[A](run: String => A)
case class CoPersist[A](run: String => A)
type Interp[A] = Tuple2K[CoLogging, CoPersist, A]

这是一个程序示例:

def prog(implicit L: Logs[Effect], P: Persists[Effect]): Free[Effect, Unit] =
  P.store("bar") >> L.log("foo")

这是翻译:

def interpretEffect(implicit CL: CoLogs[IO], CP: CoPersists[IO]): Cofree[Interp, IO[Unit]] = 
  Cofree.unfold(IO.pure(())) { a: IO[Unit] => Tuple2K(CoLogging(CL.coLog(a)), CoPersist(CP.coPersist(a))) }

我付了尽职调查并定义了仿函数以及注入含义。编译器抱怨它无法找到实例cats.Functor[[A]cats.data.Tuple2K[example.CoLogging,example.CoPersist,A]],即使我正在导入cats.data.Tuple2K._ where the instance is implicitly defined

我无法看清我做错了什么,一定是蠢事。你有什么主意吗?可以看到所有代码in this gist

2 个答案:

答案 0 :(得分:1)

<击>

<击>
  

编译器抱怨它无法找到实例   cats.Functor[[A]cats.data.Tuple2K[example.CoLogging,example.CoPersist,A]],   即使我正在导入cats.data.Tuple2K._ where the instance is implicitly defined

如果定义Functor[Tuple2K[F, G, ?]]Tuple2KInstances8#catsDataFunctorForTuple2K,则Functor[F]定义

Functor[G]。问题是Functor[CoLogging]Functor[CoPersist]不是。

添加

object CoLogging {
  implicit val coLoggingFunctor: Functor[CoLogging] = new Functor[CoLogging] {
    override def map[A, B](fa: CoLogging[A])(f: A => B): CoLogging[B] = CoLogging(fa.run andThen f)
  }
}

object CoPersist {
  implicit val coPersistFunctor: Functor[CoPersist] = new Functor[CoPersist] {
    override def map[A, B](fa: CoPersist[A])(f: A => B): CoPersist[B] = CoPersist(fa.run andThen f)
  }
}

,一切都应该编译。

事情是隐含的顺序。将对象functors移动到开头,所有内容都应该编译。

答案 1 :(得分:0)

要在代码中使用cat仿函数,您需要尝试在build.sbt文件中添加设置。

scalacOptions += "-Ypartial-unification"

也许这会让你感到高兴。这是scala编译器https://issues.scala-lang.org/browse/SI-2712

的限制