是什么 ?类型?

mus*_*oom 6 scala scala-cats

我正在尝试为Monad具有多个类型参数的类型实现cat 实例.我看着猫的Either实例,看看它是如何在那里完成的.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)
    }
}
Run Code Online (Sandbox Code Playgroud)

它无法编译错误: error: not found: type ?

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

Rüd*_*ehn 9

它是所谓类型lambdas的特殊语法,由种类的投影机插件添加.

Either[A, ?]
Run Code Online (Sandbox Code Playgroud)

是一个快捷方式

({type L[X] = Either[A, X]})#L
Run Code Online (Sandbox Code Playgroud)

整个代码都是荒谬的

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

类型lambda看起来很可怕,但它们本质上是一个非常简单的概念.你有一个需要两个类型参数的东西,比如Either[A, B].您想为Either提供monad实例,但trait Monad[F[_]]只接受一个类型参数.但原则上没有问题,因为你的monad实例只关心第二个("右")类型参数.类型lambda只是一种"修复"第一个类型参数的方法,因此您具有正确的形状.

如果你在价值水平上做同样的事情,你甚至不会考虑它两次.你有两个参数的功能

val f: (Int, Int) => Int = ...
Run Code Online (Sandbox Code Playgroud)

你要传递给f的东西,只需要1个参数

def foo(x: Int => Int) = ...
Run Code Online (Sandbox Code Playgroud)

使事情适合的唯一方法是修复其中一个参数

foo(x => f(1, x))
Run Code Online (Sandbox Code Playgroud)

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