Search code examples
scalascala-cats

Can't call map method in function with cats library


I'm reading Advanced Scala With Cats. I stuck on this example at functor description (page 59):

object FunctorsDemo extends App {

  import cats.instances.function._
  import cats.syntax.functor._

  val func1 = (x: Int) => x.toDouble
  val func2 = (y: Double) => y * 2

  val func3 = func1.map(func2) // wrong line for me

}

In book everything is okay, but I have this exception:

Error:(10, 21) value map is not a member of Int => Double
  val func3 = func1.map(func2)

Can't understand what I'm doing wrong.


Solution

  • You have encountered a bug in Scala's type inference, the partial unification bug.

    Add this to your build.sbt:

    scalacOptions += "-Ypartial-unification"
    

    There's a good writeup about it here if you're interested: https://gist.github.com/djspiewak/7a81a395c461fd3a09a6941d4cd040f2