2

我正在阅读关于无标签 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 使用的版本不兼容。

我还尝试删除我对猫的依赖并仅导入 monix,以便 monix 引入它自己的猫版本。但即使这样也无法编译。

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
4

1 回答 1

5

这些错误是由您的依赖项之间的不兼容引起的。例如,当您尝试使用二进制不兼容的时,monix2.3.3依赖于猫。0.9.01.2.0

您应该尝试升级monix3.x或降级cats0.9.0.

PS 从猫0.9.0到猫的过渡1.x有很多重大变化,您必须确保您使用的所有库都是针对相同(或至少二进制兼容)版本的猫编译的。

于 2018-08-04T15:40:07.297 回答