scalatypeclassscala-catshigher-kinded-typeshigher-order-types

Functor implementation for types with more than one type


let's say I have:

trait Get[F[_], A, B]{
  def get(a:A): F[B]
}

I want to be able to map over the result type B, ie I want to be able to do:

val getFoo: Get[IO, String, Foo] = ???
val foo2Bar: Foo => Bar = ???
val getBar: Get[IO, String, Bar] = getFoo.map(foo2Bar)

I understand that I should implement a Functor instance for Get but I am struggling as I don't know what type signature to use.

I tried the following:

but they don't seem to be of the right type as I can't seem to use the functor syntax extension as illustrated at the top. What is the right way of expressing the type here? What would be the equivalent type be if I use the kind-projector plugin?


Solution

  • Try

    import cats.syntax.functor._
    
    implicit def functor[F[_]: Functor, A]: Functor[Get[F, A, ?]] = new Functor[Get[F, A, ?]] {
      override def map[B, B1](fa: Get[F, A, B])(f: B => B1): Get[F, A, B1] = a => fa.get(a).map(f)
    }