泛型中奇怪的嵌套结构类型

Rob*_*mba 3 scala structural-typing scalaz

有人可以解释嵌套在泛型中的结构类型的奇怪结构:

implicit def Function1Functor[R]: Functor[({type ?[?]=(R) => ?})#?] = 
  new Functor[({type ?[?]=(R) => ?})#?] ....
Run Code Online (Sandbox Code Playgroud)

此示例来自Scalaz库:Functor.scala

为什么需要这种结构呢?写起来并不简单:

 implicit def Function1Functor[R,A]: Functor[R =>A]
Run Code Online (Sandbox Code Playgroud)

要么

 implicit def Function1Functor[R,A]: Functor[Function1[R,A]]
Run Code Online (Sandbox Code Playgroud)

Ben*_*mes 7

Functor类型构造函数的签名显示它是使用另一个一元类型构造函数进行参数化的F:

trait Functor[F[_]] extends InvariantFunctor[F]
Run Code Online (Sandbox Code Playgroud)

既不R => A也不Function1[R,A]是类型构造; 他们没有参数.

但是在:

type ?[?] = (R) => ?
Run Code Online (Sandbox Code Playgroud)

?是一个带有一个参数的类型构造函数?.(R已在此上下文中定义.)

语法({type ?[?]=(R) => ?})#?称为类型lambda.这是一种语法技巧,允许类型别名内联创建并通过投影引用,因此整个表达式可用于需要类型的地方.