简体   繁体   中英

could not find implicit value for parameter functor: cats.Functor[Some]

I am trying to follow a simple example from the scala with cats ebook. here is my code

import cats.Semigroupal
import cats.instances.option._
import cats.syntax.apply._
import cats.implicits._
case class Name(fName: String, lName: String)
(Some("foo"), Some("bar")).mapN(Name.apply)

I get the error

cmd4.sc:1: could not find implicit value for parameter functor: cats.Functor[Some]
val res4 = (Some("foo"), Some("bar")).mapN(Name.apply)

I also tried importing

cats.functor._
cats.syntax.functor._

The problem is that Functor is invariant in its type argument, so you need to make the Scala compiler treat the type of the tuple as (Option[String], Option[String]) instead of (Some[String], Some[String]) . You can do this using

("foo".some, "bar".some).mapN(Name.apply)

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