有人可以解释一下嵌套在泛型中的结构类型的奇怪构造吗:
implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] =
new Functor[({type λ[α]=(R) => α})#λ] ....
这个例子来自Scalaz库:函子.scala http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.2/doc.sxr/scalaz/Functor.scala.html#14966
为什么那里需要这样的建设?写起来不会更简单:
implicit def Function1Functor[R,A]: Functor[R =>A]
or
implicit def Function1Functor[R,A]: Functor[Function1[R,A]]
的签名Functor
类型构造函数显示它是用另一个一元类型构造函数参数化的F
:
trait Functor[F[_]] extends InvariantFunctor[F]
Neither R => A
nor Function1[R,A]
是类型构造函数;他们不带任何参数。
然而在:
type λ[α] = (R) => α
λ
是一个带有一个参数的类型构造函数,α
. (R
已经在此上下文中定义。)
语法({type λ[α]=(R) => α})#λ
称为 lambda 类型。这是一个语法技巧,允许内联创建类型别名并通过投影引用,因此可以在需要类型的地方使用整个表达式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)