softshipper
softshipper

Reputation: 34071

How do I know, which implicit that is needs?

I have a working code:

final case class Services[F[_]](c: Client[F], fooUrl: String)
                               (implicit cf: ConcurrentEffect[F]) {

  private val dsl = Http4sDsl[F]

  import dsl._

  def routes: HttpRoutes[F] = HttpRoutes.of[F] {
    case GET -> Root / "call" =>
      c.get(fooUrl) { res =>
        Applicative[F].pure {
          Response[F]().withEntity {
            res
              .body
              .through(text.utf8Decode)
              .map(msg => "Forwarded through Boo" |+| msg)
          }
        }
      }
  }

When I would delete the implicit import implicit cf: ConcurrentEffect[F], then compiler will complain:

[error] /home/developer/scala/boo-service/src/main/scala/com/sweetsoft/Services.scala:29:20: could not find implicit value for parameter instance: cats.Applicative[F]
[error]         Applicative[F].pure {
[error]                    ^
[error] /home/developer/scala/boo-service/src/main/scala/com/sweetsoft/Services.scala:26:48: could not find implicit value for evidence parameter of type cats.Defer[F]
[error]   def routes: HttpRoutes[F] = HttpRoutes.of[F] {
[error]                                                ^
[error] two errors found

How do I know, the type class implicit cf: ConcurrentEffect[F] is missing?

Upvotes: 0

Views: 1483

Answers (1)

oybek
oybek

Reputation: 650

I think the answer you can get if you will look to sources of cats-effect library:

Lets go to ConcurrentEffect definition:

trait ConcurrentEffect[F[_]] extends Concurrent[F] with Effect[F] {...

Hmm, now let's look at Concurrent:

trait Concurrent[F[_]] extends Async[F] {...

Async:

trait Async[F[_]] extends Sync[F] with LiftIO[F] {...

Next:

trait Sync[F[_]] extends Bracket[F, Throwable] with Defer[F] {

Now we can conclude that ConcurrentEffect extends Defer

The same try to do with Applicative yourself ;)

To handle such an errors fast - I think you have to understand extensions hierarchy - so you can fastly figure out which typeclass you need

Upvotes: 3

Related Questions