简体   繁体   中英

folding of list of options is compile error

why the second folding compile error?

I think the first folding and the second one are completely same. But the second one arise compile error.

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)))

compile error message is

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

I expect the code is compiled.

Doc tells that you need to import implicits as well

import cats.implicits._

here is the answer regarding Some and Option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM