cats icon indicating copy to clipboard operation
cats copied to clipboard

Scala 3: No given instance of type cats.Applicative[[_] =>> Any] was found for parameter F of method apply in class PurePartiallyApplied

Open froth opened this issue 2 years ago • 1 comments

I am currently migrating code from scala 2.13 to Scala 3 and found a weird issue. I am not sure whether this is a bug or not. And also sure whether it should be reported here or on http4s. Opening to be sure:

The following code compiles under scala 2.13:

import org.http4s.server.AuthMiddleware
import cats.data.Kleisli
import cats.data.OptionT
val middleware = AuthMiddleware(Kleisli { _ => OptionT.pure { "foo" } })

But reports compile errors under scala3:

[error] -- [E172] Type Error: /tmp/quickstart/src/main/scala/com/example/quickstart/Main.scala:12:71 
[error] 12 |  val middleware = AuthMiddleware(Kleisli { _ => OptionT.pure { "foo" } })
[error]    |                                                                       ^
[error]    |No given instance of type cats.Applicative[[_] =>> Any] was found for parameter F of method apply in class PurePartiallyApplied
[error]    |
[error]    |One of the following imports might make progress towards fixing the problem:
[error]    |
[error]    |  import fs2.Compiler.Target.forConcurrent
[error]    |  import fs2.Compiler.Target.forSync
[error]    |
[error] -- [E172] Type Error: /tmp/quickstart/src/main/scala/com/example/quickstart/Main.scala:12:74 
[error] 12 |  val middleware = AuthMiddleware(Kleisli { _ => OptionT.pure { "foo" } })
[error]    |                                                                          ^
[error]    |No given instance of type cats.Monad[[_] =>> Any] was found for an implicit parameter of method apply in object AuthMiddleware
[error]    |
[error]    |One of the following imports might make progress towards fixing the problem:
[error]    |
[error]    |  import fs2.Compiler.Target.forConcurrent
[error]    |  import fs2.Compiler.Target.forSync
[error]    |

The following variant compiles under scala 3:

val response: Kleisli[OptionT[IO, *], Request[IO], String] = Kleisli { _ => OptionT.pure { "foo" } }
val middleware = AuthMiddleware(response)

froth avatar Aug 24 '23 09:08 froth

@froth I faced the same issue whenI was trying to enable CORS using tapir

private val customServerOptions = Http4sServerOptions.customiseInterceptors
    .corsInterceptor(
      CORSInterceptor.customOrThrow(
        CORSConfig.default
          .allowAllOrigins
          .allowMethods(Method.GET, Method.POST, Method.PUT, Method.OPTIONS, Method.DELETE)
          .allowHeaders("Authorization", "Content-Type", "X-Requested-With")
          .allowCredentials
          .maxAge(42.seconds)
      )
    )
    .options
No given instance of type cats.effect.kernel.Sync[F] was found for a context parameter of method customiseInterceptors in object Http4sServerOptions.
I found:

    cats.effect.kernel.Sync.syncForKleisli[F², R](
      cats.effect.kernel.Sync.syncForEitherT[F³, E](
        cats.effect.kernel.Sync.syncForStateT[F⁴, S](
          cats.effect.kernel.Sync.syncForOptionT[F⁵](
            cats.effect.kernel.Sync.syncForReaderWriterStateT[F⁶, R², L, S²](
              cats.effect.kernel.Sync.syncForWriterT[F⁷, L²](
                cats.effect.kernel.Sync.syncForIorT[F⁸, L³](
                  /* missing */summon[cats.effect.kernel.Sync[F⁸]], ???),
              ???),
            ???)
          )
        )
      )
    )

But no implicit values were found that match type cats.effect.kernel.Sync[F⁸]

where:    F  is a type variable with constraint <: [_] =>> Any
          F² is a type variable with constraint <: [_] =>> Any
          F³ is a type variable with constraint <: [_] =>> Any
          F⁴ is a type variable with constraint <: [_] =>> Any
          F⁵ is a type variable with constraint <: [_] =>> Any
          F⁶ is a type variable with constraint <: [_] =>> Any
          F⁷ is a type variable with constraint <: [_] =>> Any
          F⁸ is a type variable with constraint <: [_] =>> Any
          L  is a type variable
          L² is a type variable
          L³ is a type variable
          R  is a type variable
          R² is a type variable
          S  is a type variable
          S² is a type variable
.
  private val customServerOptions = Http4sServerOptions.customiseInterceptors

UPDATE This works:

Http4sServerOptions.customiseInterceptors[IO]

irajhedayati avatar Apr 27 '25 04:04 irajhedayati