3

I have this typeclass

import simulacrum._
@typeclass trait Functor[F[_]] {
    def map[A, B](fa: F[A])(f: A => B) : F[B]
    def lift[A, B](fa: F[A])(f: A => B) : F[A] => F[B] = fa => map(fa)(f)
    def as[A, B](fa: F[A], b: => B) : F[B] = map(fa)(_ => b)
    def void[A](fa: F[A]) : F[Unit] = as(fa, ())
}

and this is the implementation

object Functor {
    implicit val listFunctor: Functor[List] = new Functor[List] {
        def map[A, B](fa: List[A])(f: A => B) = fa.map(f)
    }
    implicit def functionFunctor[X]: Functor[X => ?] = new Functor[X => ?] {
        def map[A, B](fa : X => A)(f : A => B) = fa andThen f
    }
}

I can easily discover the List implicit implementation as

object Chapter1 extends App {
    import Functor.ops._
    List(1, 2, 3).as("foo").foreach(println)
}

The above works perfectly fine. I can also do

object Chapter1 extends App {
    import Functor._
    val func : Int => String = implicitly[Functor[Int => ?]].map(_ + 2)(_.toString)
    println(func(5))
}

But when I try

object Chapter1 extends App {
    import Functor.ops._
    val x : Int => Int = _ + 2
    val y : Int => String = x.map(_.toString)
}

It doesn't find my implicit implementation and says that the value map is not a member of Int => Int

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373

1 Answers1

3

Compiler can't see that Int => Int is Int => ? applied to Int.

Add

scalacOptions += "-Ypartial-unification"

to build.sbt.

It's necessary for normal work with higher-kinded types with Cats, Scalaz or manually.

By the way, there's no sense to import Functor._

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66