【问题标题】:scala cats applicative failed to compilescala 猫应用程序无法编译
【发布时间】:2022-01-17 21:14:39
【问题描述】:

我尝试关注https://typelevel.org/cats/typeclasses/applicative.html

trait Applicative[F[_]] extends Functor[F] {
    def product[A, B](fa: F[A], fb: F[B]): F[(A, B)]

    def pure[A](a: A): F[A]
  }

  // Example implementation for right-biased Either
  implicit def applicativeForEither[L]: Applicative[Either[L, *]] = new Applicative[Either[L, *]] {
    def product[A, B](fa: Either[L, A], fb: Either[L, B]): Either[L, (A, B)] = (fa, fb) match {
      case (Right(a), Right(b)) => Right((a, b))
      case (Left(l) , _       ) => Left(l)
      case (_       , Left(l) ) => Left(l)
    }

    def pure[A](a: A): Either[L, A] = Right(a)

    def map[A, B](fa: Either[L, A])(f: A => B): Either[L, B] = fa match {
      case Right(a) => Right(f(a))
      case Left(l)  => Left(l)
    }
  }

编译失败,报错:

未找到:输入 * 隐式 def applicativeForEither[L]: Applicative[Either[L, *]] = new Applicative[Either[L, *]] {

在 cat's 中它使用 '?'而不是'*'(例如EitherTFunctor),但当我复制粘贴它时它也无法编译。

我应该怎么做才能解决它?

【问题讨论】:

    标签: scala generics scala-cats kind-projector


    【解决方案1】:

    要使您的代码可与类型参数中的星号编译,您应该将 kind-projector 插件添加到您的 build.sbt 文件或 plugins.sbt 文件中:

    addCompilerPlugin("org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full)
    

    README.MD阅读更多关于kind-projector的信息

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 2021-11-01
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多