Kev*_*ith 6 scala partial-application type-constructor kind-projector
取自typelevel/kind-projector,它们之间的区别是什么:
// partially-applied type named "IntOrA"
type IntOrA[A] = Either[Int, A]
Run Code Online (Sandbox Code Playgroud)
和
// type projection implementing the same type anonymously (without a name).
({type L[A] = Either[Int, A]})#L
Run Code Online (Sandbox Code Playgroud)
?
它们是等价的吗?
正如评论中所说,它们几乎是等效的。
\n\n假设你有一个类trait Super[F[_]] {},并且你想在其中实现它F[x] = Either[Int, x]\n你可以写:
type IntOrA[A] = Either[Int, A]\nclass B extends Super[IntOrA] {}\nRun Code Online (Sandbox Code Playgroud)\n\n但如果你想要一个衬垫,你可以写:
\n\nclass B extends Super[({type L[A] = Either[Int, A]})#L] {}\nRun Code Online (Sandbox Code Playgroud)\n\n或者使用 kind-projector 你可以这样写:
\n\nclass B extends Super[\xce\xbb(A => Either[Int, A])] {}\nRun Code Online (Sandbox Code Playgroud)\n\n甚至:
\n\nclass B extends Super[Either[Int, ?]] {}\nRun Code Online (Sandbox Code Playgroud)\n\n除了将其设为一行并使用匿名类型之外,没有其他区别。
\n| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |