【问题标题】:Understanding Type Projection了解类型投影
【发布时间】:2016-03-03 19:44:04
【问题描述】:

取自typelevel/kind-projector,有什么区别:

// partially-applied type named "IntOrA"
type IntOrA[A] = Either[Int, A]

// type projection implementing the same type anonymously (without a name).
({type L[A] = Either[Int, A]})#L

?

它们是等价的吗?

【问题讨论】:

  • 最后这个#L是什么?
  • @YuvalItzchakov 以及#L 访问刚刚在里面创建的类型成员
  • @Łukasz 还有#有什么用?
  • @YuvalItzchakov # 是类型投影。它允许您访问任何依赖于路径的类型,L 是并将其视为不依赖于路径,即通过路径依赖类型,内部类型在使用. 访问时不相等,但在使用# 访问时是相等的。查看更多stackoverflow.com/questions/9443004/…

标签: scala partial-application type-constructor kind-projector


【解决方案1】:

正如评论中所说,它们几乎是等价的。

假设你有一个类trait Super[F[_]] {},并且你想在F[x] = Either[Int, x] 的地方实现它 你可以写:

type IntOrA[A] = Either[Int, A]
class B extends Super[IntOrA] {}

但如果你想要一个单行,你可以写:

class B extends Super[({type L[A] = Either[Int, A]})#L] {}

或者使用 kind-projector 你可以这样写:

class B extends Super[λ(A => Either[Int, A])] {}

甚至:

class B extends Super[Either[Int, ?]] {}

除了将其设为单行并让这种类型匿名之外,没有其他区别。

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 2016-02-15
    • 2021-06-14
    • 1970-01-01
    • 2012-08-29
    • 2021-09-19
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多