找不到 Tuple2K 的仿函数实例
Cannot find a functor instance for Tuple2K
我有一个玩具 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.
要在您的代码中使用猫函子,您需要尝试在您的 build.sbt 文件中添加设置。
scalacOptions += "-Ypartial-unification"
也许这会帮助你。这是 scala 编译器的限制 https://issues.scala-lang.org/browse/SI-2712
The compiler complains that it cannot find an instance
cats.Functor[[A]cats.data.Tuple2K[example.CoLogging,example.CoPersist,A]]
,
even though I am importing cats.data.Tuple2K._
where the instance is implicitly defined.
如果定义了 Functor[F]
和 Functor[G]
,则 Functor[Tuple2K[F, G, ?]]
通过 Tuple2KInstances8#catsDataFunctorForTuple2K
定义。问题是 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
移动到开头,一切都应该编译。
我有一个玩具 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.
要在您的代码中使用猫函子,您需要尝试在您的 build.sbt 文件中添加设置。
scalacOptions += "-Ypartial-unification"
也许这会帮助你。这是 scala 编译器的限制 https://issues.scala-lang.org/browse/SI-2712
如果定义了The compiler complains that it cannot find an instance
cats.Functor[[A]cats.data.Tuple2K[example.CoLogging,example.CoPersist,A]]
, even though I am importingcats.data.Tuple2K._
where the instance is implicitly defined.
Functor[F]
和 Functor[G]
,则 Functor[Tuple2K[F, G, ?]]
通过 Tuple2KInstances8#catsDataFunctorForTuple2K
定义。问题是 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
移动到开头,一切都应该编译。