我有以下代码:
import cats._
import cats.Functor
object StudyIt {
def main(args: Array[String]): Unit = {
val one: Int = 1
val a = Functor[Id].map(one)(_ + 1)
println(a)
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,map期望的类型Id[Int],但我只是过去Int到map,为什么它是可能的吗?
那是因为Id它只是一个类型别名,它在运行时完全内联.它看起来像这样:
type Id[A] = A
Run Code Online (Sandbox Code Playgroud)
因此,在本质Id[Int]和Int是完全相同的类型.
| 归档时间: |
|
| 查看次数: |
152 次 |
| 最近记录: |