Skip to content

Inference issue when an implicit method is promoted to an implicit function #4725

@kubukoz

Description

@kubukoz
import language.higherKinds

object Main {
  trait Eq[A]  
  
  case class Cofree[S[_], A](head: A, tail: Cofree[S, A]) //simplified
  

  implicit val intEq: Eq[Int] = null
  implicit def forList[A : Eq]: Eq[List[A]] = ???
  
  
  implicit def eqCofree[S[_], A : Eq](implicit
    S: implicit Eq[A] => Eq[S[A]]): Eq[Cofree[S, A]] = ???
  
  
  val a = eqCofree[List, Int]
}

This fails with:

-- Error: /tmp/scastie6251223766744740367/src/main/scala/main.scala:16:29 ------
16 |  val a = eqCofree[List, Int]
   |                             ^
   |no implicit argument of type implicit Main.Eq[Int] => Main.Eq[List[Int]] was found for parameter S of method eqCofree in object Main.
   |I found:
   |
   |    {
   |      def $anonfun(implicit evidence$4: Main.Eq[Int]): Main.Eq[List[Int]] = 
   |        {
   |          def $anonfun(evidence$1: Main.Eq[Any]): Main.Eq[List[Any]] = 
   |            Main.forList[Any](evidence$1)
   |          closure($anonfun)
   |        }
   |      closure($anonfun)
   |    }
   |
   |But method forList in object Main does not match type implicit Main.Eq[Int] => Main.Eq[List[Int]]

but if you use eqCofree(intEq, forList), it compiles.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions