3

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

implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] = 
  new Functor[({type λ[α]=(R) => α})#λ] ....

这个例子来自 Scalaz 库:Functor.scala

为什么那里需要这种结构?写起来不会更简单:

 implicit def Function1Functor[R,A]: Functor[R =>A]

或者

 implicit def Function1Functor[R,A]: Functor[Function1[R,A]]
4

1 回答 1

7

类型构造函数的签名Functor表明它是用另一个一元类型构造函数参数化的F

trait Functor[F[_]] extends InvariantFunctor[F]

类型构造函数也不R => A是;Function1[R,A]他们不带参数。

然而在:

type λ[α] = (R) => α

λ是一个接受一个参数的类型构造函数,α. (R已在此上下文中定义。)

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

于 2011-11-23T14:24:46.010 回答