Search code examples
scalaslickscala-cats

Could not find implicit value for parameter F: cats.Applicative[F]


I'm currently going through a tutorial on cats.

The tutorial gives me a repository of cat breads (uses Slick) which I'm not supposed to edit, then asks me to implement different methods. Unfortunately I'm stuck on something which seems pretty obvious.

I am given the method

  def findByName(name: String)(implicit ec: ExecutionContext): DBIO[Option[Breed]] = {
    query.filter(_.name === name).result.headOption
  }

in breedsRepository and I'm supposed to implement the following method:

def findBreed(name: Either[Int, String]): DBIO[Either[Int, Breed]] = ???

I thought about:

def findBreed(name: Either[Int, String]): DBIO[Either[Int, Breed]] =
    name.traverse(n => breedsRepository.findByName(n).map(_.get))

But I get the error could not find implicit value for parameter F: cats.Applicative[F] Could anyone help me out?


Solution

  • The following code compiles:

    import slick.jdbc.PostgresProfile.api._
    import scala.concurrent.ExecutionContext
    import scala.concurrent.ExecutionContext.Implicits.global
    import cats.implicits._
    import com.rms.miu.slickcats.DBIOInstances._
    
    object breedsRepository {   
      case class Breed(name: String, price: Double)
    
      class BreedTable(tag: Tag) extends Table[Breed](tag, "BREEDS") {
        def name = column[String]("NAME", O.PrimaryKey)
        def price = column[Double]("PRICE")
        def * = (name, price) <> (Breed.tupled, Breed.unapply)
      }
      val query = TableQuery[BreedTable]
    
      def findByName(name: String)(implicit ec: ExecutionContext): DBIO[Option[Breed]] = {
        query.filter(_.name === name).result.headOption
      }
    
      def findBreed(name: Either[Int, String]): DBIO[Either[Int, Breed]] =
        name.traverse[DBIO, Int, Breed](n => findByName(n).map(_.get))
    }
    

    build.sbt

    name := "slickdemo"
    
    version := "0.1"
    
    scalaVersion := "2.12.6"
    
    scalacOptions += "-Ypartial-unification"
    
    libraryDependencies ++= Seq(
      "com.typesafe.slick" %% "slick" % "3.2.3",
      "org.slf4j" % "slf4j-nop" % "1.6.4",
      "com.typesafe.slick" %% "slick-hikaricp" % "3.2.3"
    )
    
    libraryDependencies += "org.typelevel" %% "cats-core" % "1.0.1"
    
    libraryDependencies += "com.rms.miu" %% "slick-cats" % "0.7.1.1"