【问题标题】:Weird nested structural type in generics泛型中奇怪的嵌套结构类型
【发布时间】:2011-11-23 14:18:12
【问题描述】:

谁能解释嵌套在泛型中的结构类型的奇怪构造:

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]]

【问题讨论】:

    标签: scala scalaz structural-typing


    【解决方案1】:

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

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

    R => AFunction1[R,A] 都不是类型构造函数;它们不带参数。

    但是在:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2023-01-10
      相关资源
      最近更新 更多