了解类型投影

问题描述 投票:4回答:1

来自non/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

?

它们是等价的吗?

scala kind-projector
1个回答
3
投票

它们几乎相同,正如评论中所说的那样。

假设你有一个类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] {}

或者使用亲切的投影仪,你可以写它:

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

甚至:

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

除了使它成为一行并且具有这种类型的匿名之外没有其他区别。

© www.soinside.com 2019 - 2024. All rights reserved.