0

我尝试关注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 中,它使用 '?' 而不是'*'(例如EitherTFunctor),但当我复制粘贴它时它也无法编译。

我应该怎么做才能修复它?

4

1 回答 1

3

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

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

在README.MD上阅读有关 kind-projector 的更多信息

于 2021-12-14T14:58:39.633 回答