【发布时间】:2020-10-17 23:44:24
【问题描述】:
我正在浏览“Scala with cats”。在 3.5.2(第 58 页,底部)中有一个示例:
def doMath[F[_]](start: F[Int])(implicit functor: Functor[F]): F[Int] =
start.map(n => n + 1 * 2)
而且用法非常简单:
import cats.instances.option._ // for Functor
import cats.instances.list._
// for Functor
doMath(Option(20))
// res3: Option[Int] = Some(22)
doMath(List(1, 2, 3))
// res4: List[Int] = List(3, 4, 5)
我应该如何理解方法签名(F[_])中的类型构造函数?前几页说过,应该提供类型参数来创建类型。这里整个(F[_])是一个类型参数,看起来_是一个通配符,所以编译器可以推断F的类型参数。
【问题讨论】:
标签: scala types scala-cats category-theory type-constructor