选项列表的折叠是编译错误



为什么会出现第二次折叠编译错误?

我认为第一种折叠和第二种完全相同。但是第二种会导致编译错误。

import cats._
import cats.data._
import cats.syntax.all._
val somes : List[Option[Int]] = List(Some(1), Some(2))
Foldable[List].fold(somes)
Foldable[List].fold(List(Some(1), Some(2)))

编译错误信息

No given instance of type cats.kernel.Monoid[Some[Int]] was found for parameter A of method fold in trait Foldable

我希望代码被编译。

文档告诉您还需要导入隐式

import cats.implicits._

以下是关于Some和Option的答案

最新更新