是什么 ?类型?

What is the ? type?

我正在尝试为具有多个类型参数的类型实现一个 cats Monad 实例。我查看了 cats Either 实例,看看它是如何完成的。 cats 的 Either Monad 部分实例代码复制如下:

import cats.Monad

object EitherMonad {
  implicit def instance[A]: Monad[Either[A, ?]] =
    new Monad[Either[A, ?]] {
      def pure[B](b: B): Either[A, B] = Right(b)

      def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] =
        fa.right.flatMap(f)
    }
}

编译失败,错误:error: not found: type ?

什么是 ? 类型?在为我自己的类型创建实例时如何使用它?

它是由kind projector plugin添加的所谓类型lambdas的特殊语法。

Either[A, ?]

的快捷方式
({type L[X] = Either[A, X]})#L

整个代码脱糖到

import cats.Monad

object EitherMonad {
  implicit def instance[A]: Monad[({type L[X] = Either[A, X]})#L] = new Monad[({type L[X] = Either[A, X]})#L] {
    def pure[B](b: B): Either[A, B] = Right(b)

    def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] =
      fa.right.flatMap(f)
  }
}

类型 lambda 看起来很可怕,但它们本质上是一个非常简单的概念。你有一个东西需要两个类型参数,比如 Either[A, B]。你想为 Either 提供一个 monad 实例,但是 trait Monad[F[_]] 只接受一个类型参数。但原则上这没关系,因为你的 monad 实例无论如何只关心第二个("right")类型参数。类型 lambda 只是 "fix" 第一个类型参数的一种方式,所以你有正确的形状。

如果你在价值层面做同样的事情,你甚至不会考虑两次。你得到了一个有两个参数的函数

val f: (Int, Int) => Int = ...

还有你想将 f 传递给的东西,它只需要 1 个参数

def foo(x: Int => Int) = ...

使事情合适的唯一方法是修正其中一个论点

foo(x => f(1, x))

这正是类型 lambda 在类型级别所做的。