类路径中缺少符号“类型cats.MonadFilter”

时间:2018-08-01 06:00:54

标签: scala scala-cats monix

我正在阅读tagless final上的本教程。

基于此,我将依赖项定义为

object Dependencies {
  lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
  lazy val cats = "org.typelevel" %% "cats-core" % "1.2.0"
  lazy val monix = "io.monix" %% "monix" % "2.3.3"
  lazy val monixCats = "io.monix" %% "monix-cats" % "2.3.3"
}

以下是我的代码

// future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._

// cats
import cats.Monad
import cats.implicits._

// monix
import monix.eval.Task
import monix.cats._
import monix.cats.reverse._


trait ProductRepository[M[_]] {
  def findProduct(productId: ProductId) : M[Option[Product]]
  def saveProduct(product: Product) : M[Unit]
  def incrementProductSales(productId: ProductId, quantity: Long) : M[Unit]
}

class ProductRepositoryWithFuture extends ProductRepository[Future] {
  def findProduct(productId: ProductId) : Future[Option[Product]] = {
    Future.successful(Some(Product(productId, "foo")))
  }
  def saveProduct(product: Product) : Future[Unit] = {
    Future.successful()
  }
  def incrementProductSales(productId: ProductId, quanity: Long) : Future[Unit] = {
    Future.successful()
  }
}

class ProductRepositoryWithTask extends ProductRepository[Task] {
  def findProduct(productId: ProductId) : Task[Option[Product]] = {
    Task.now(Some(Product(productId, "foo")))
  }
  def saveProduct(product: Product) : Task[Unit] = {
    Task.unit
  }
  def incrementProductSales(productId: ProductId, quantity: Long) : Task[Unit] = {
    Task.unit
  }
}

但是我遇到了很多错误。看来我使用的猫的版本与Monix使用的猫不兼容。

我还试图删除对cats的依赖,只是导入了monix,以便monix引入自己的cats版本。但即使这样也无法编译。

error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:54:24: Symbol 'type cats.MonadFilter' is missing fromthe classpath.
[error] This symbol is required by 'method monix.cats.MonixToCatsCore7.monixToCatsMonadFilter'.
[error] Make sure that type MonadFilter is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'MonixToCatsCore7.class' was compiled against an incompatible version of cats.
[error]       repo.findProduct(id).flatMap{
[error]                        ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:54:23: diverging implicit expansion for type monix.types.Comonad[M]
[error] starting with method catsToMonixComonad in trait CatsCoreToMonix5
[error]       repo.findProduct(id).flatMap{
[error]                       ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:54:28: value flatMap is not a member of type parameter M[Option[example.Application.Product]]
[error]       repo.findProduct(id).flatMap{
[error]                            ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:56:30: value copy is not a member of Any
[error]           val newProduct = p.copy(name = name)
[error]                              ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:56:40: reassignment to val
[error]           val newProduct = p.copy(name = name)
[error]                                        ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:57:27: diverging implicit expansion for type monix.types.MonadError[M,E]
[error] starting with method catsToMonixMonadError in trait CatsCoreToMonix3
[error]           repo.saveProduct(newProduct).map(_ => Some(p))
[error]                           ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:57:40: value map is not a member of type parameter M[Unit]
[error]           repo.saveProduct(newProduct).map(_ => Some(p))
[error]                                        ^
[error] /Users/foobar/code/tagless/src/main/scala/example/Hello.scala:59:16: diverging implicit expansion for type cats.Comonad[M]
[error] starting with method monixToCatsComonad in trait MonixToCatsCore5
[error]           Monad[M].pure(None)
[error]                ^
[error] 8 errors found

1 个答案:

答案 0 :(得分:1)

这些错误是由您的依赖项之间的不兼容引起的。 例如,当您尝试使用二进制不兼容的2.3.3时,monix 0.9.0取决于猫1.2.0

您应该尝试将monix升级到3.x或将cats降级到0.9.0

P.S。从cat 0.9.01.x的过渡有很多changes,并且您必须确保所使用的所有库都针对相同的库进行编译(或至少与二进制文件兼容)猫的版本。